/******************************************************************************
	This file is a part of the Snapshot Bioinformatics Project

	Copyright (C) 2012-2013 Jarom Schow

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

#include "SqlScripts.h"

/**
**/
const char *c_my_sql_create_tables =
"CREATE TABLE biodatabase (\n"
"  	biodatabase_id 	INT(10) UNSIGNED NOT NULL auto_increment,\n"
"  	name           	VARCHAR(128) BINARY NOT NULL,\n"
"	authority	VARCHAR(128),\n"
"	description	TEXT,\n"
"	PRIMARY KEY (biodatabase_id),\n"
"  	UNIQUE (name)\n"
") ENGINE=INNODB;\n"

"CREATE INDEX db_auth on biodatabase(authority);\n"

"CREATE TABLE taxon (\n"
"       taxon_id		INT(10) UNSIGNED NOT NULL auto_increment,\n"
"       ncbi_taxon_id 	INT(10),\n"
"       parent_taxon_id	INT(10) UNSIGNED,\n"
"       node_rank	VARCHAR(32),\n"
"       genetic_code	TINYINT UNSIGNED,\n"
"       mito_genetic_code TINYINT UNSIGNED,\n"
"       left_value	INT(10) UNSIGNED,\n"
"       right_value	INT(10) UNSIGNED,\n"
"       PRIMARY KEY (taxon_id),\n"
"       UNIQUE (ncbi_taxon_id),\n"
"       UNIQUE (left_value),\n"
"       UNIQUE (right_value)\n"
") ENGINE=INNODB;\n"

"CREATE INDEX taxparent ON taxon(parent_taxon_id);\n"

"CREATE TABLE taxon_name (\n"
"       taxon_id		INT(10) UNSIGNED NOT NULL,\n"
"       name		VARCHAR(255) BINARY NOT NULL,\n"
"       name_class	VARCHAR(32) BINARY NOT NULL,\n"
"       UNIQUE (taxon_id,name,name_class)\n"
") ENGINE=INNODB;\n"

"CREATE INDEX taxnametaxonid ON taxon_name(taxon_id);\n"
"CREATE INDEX taxnamename    ON taxon_name(name);\n"

"CREATE TABLE ontology (\n"
"       	ontology_id        INT(10) UNSIGNED NOT NULL auto_increment,\n"
"       	name	   	   VARCHAR(32) BINARY NOT NULL,\n"
"       	definition	   TEXT,\n"
"	PRIMARY KEY (ontology_id),\n"
"	UNIQUE (name)\n"
") ENGINE=INNODB;\n"

"CREATE TABLE term (\n"
"       	term_id   INT(10) UNSIGNED NOT NULL auto_increment,\n"
"       	name	   	   VARCHAR(255) BINARY NOT NULL,\n"
"       	definition	   TEXT,\n"
"	identifier	   VARCHAR(40) BINARY,\n"
"	is_obsolete	   CHAR(1),\n"
"	ontology_id	   INT(10) UNSIGNED NOT NULL,\n"
"	PRIMARY KEY (term_id),\n"
"	UNIQUE (identifier),\n"
"          UNIQUE (name,ontology_id,is_obsolete)\n"
") ENGINE=INNODB;\n"

"CREATE INDEX term_ont ON term(ontology_id);\n"

"CREATE TABLE term_synonym (\n"
"       synonym		  VARCHAR(255) BINARY NOT NULL,\n"
"       term_id		  INT(10) UNSIGNED NOT NULL,\n"
"       PRIMARY KEY (term_id,synonym)\n"
") ENGINE=INNODB;\n"

"CREATE TABLE term_dbxref (\n"
"       	term_id	          INT(10) UNSIGNED NOT NULL,\n"
"       	dbxref_id         INT(10) UNSIGNED NOT NULL,\n"
"	rank		  SMALLINT,\n"
"	PRIMARY KEY (term_id, dbxref_id)\n"
") ENGINE=INNODB;\n"

"CREATE INDEX trmdbxref_dbxrefid ON term_dbxref(dbxref_id);\n"

"CREATE TABLE term_relationship (\n"
"        term_relationship_id INT(10) UNSIGNED NOT NULL auto_increment,\n"
"       	subject_term_id	INT(10) UNSIGNED NOT NULL,\n"
"       	predicate_term_id    INT(10) UNSIGNED NOT NULL,\n"
"       	object_term_id       INT(10) UNSIGNED NOT NULL,\n"
"	ontology_id	INT(10) UNSIGNED NOT NULL,\n"
"	PRIMARY KEY (term_relationship_id),\n"
"	UNIQUE (subject_term_id,predicate_term_id,object_term_id,ontology_id)\n"
") ENGINE=INNODB;\n"

"CREATE INDEX trmrel_predicateid ON term_relationship(predicate_term_id);\n"
"CREATE INDEX trmrel_objectid ON term_relationship(object_term_id);\n"
"CREATE INDEX trmrel_ontid ON term_relationship(ontology_id);\n"

"CREATE TABLE term_relationship_term (\n"
"        term_relationship_id INT(10) UNSIGNED NOT NULL,\n"
"        term_id              INT(10) UNSIGNED NOT NULL,\n"
"        PRIMARY KEY ( term_relationship_id ),\n"
"        UNIQUE ( term_id ) \n"
") ENGINE=INNODB;\n"

"CREATE TABLE term_path (\n"
"        term_path_id         INT(10) UNSIGNED NOT NULL auto_increment,\n"
"       	subject_term_id	     INT(10) UNSIGNED NOT NULL,\n"
"       	predicate_term_id    INT(10) UNSIGNED NOT NULL,\n"
"       	object_term_id       INT(10) UNSIGNED NOT NULL,\n"
"	ontology_id          INT(10) UNSIGNED NOT NULL,\n"
"	distance	     INT(10) UNSIGNED,\n"
"	PRIMARY KEY (term_path_id),\n"
"	UNIQUE (subject_term_id,predicate_term_id,object_term_id,ontology_id,distance)\n"
") ENGINE=INNODB;\n"

"CREATE INDEX trmpath_predicateid ON term_path(predicate_term_id);\n"
"CREATE INDEX trmpath_objectid ON term_path(object_term_id);\n"
"CREATE INDEX trmpath_ontid ON term_path(ontology_id);\n"

"CREATE TABLE bioentry (\n"
"	bioentry_id	    INT(10) UNSIGNED NOT NULL auto_increment,\n"
"  	biodatabase_id  INT(10) UNSIGNED NOT NULL,\n"
"  	taxon_id     	INT(10) UNSIGNED,\n"
"  	name		VARCHAR(40) NOT NULL,\n"
"  	accession    	VARCHAR(128) BINARY NOT NULL,\n"
"  	identifier   	VARCHAR(40) BINARY,\n"
"	division	VARCHAR(6),\n"
"  	description  	TEXT,\n"
"  	version 	SMALLINT UNSIGNED NOT NULL, \n"
"	PRIMARY KEY (bioentry_id),\n"
"  	UNIQUE (accession,biodatabase_id,version),\n"
" 	UNIQUE (identifier, biodatabase_id)\n"
") ENGINE=INNODB;\n"

"CREATE INDEX bioentry_name ON bioentry(name);\n"
"CREATE INDEX bioentry_db   ON bioentry(biodatabase_id);\n"
"CREATE INDEX bioentry_tax  ON bioentry(taxon_id);\n"

"CREATE TABLE bioentry_relationship (\n"
"    bioentry_relationship_id INT(10) UNSIGNED NOT NULL auto_increment,\n"
"    object_bioentry_id 	     INT(10) UNSIGNED NOT NULL,\n"
"   	subject_bioentry_id 	 INT(10) UNSIGNED NOT NULL,\n"
"   	term_id 		         INT(10) UNSIGNED NOT NULL,\n"
"   	rank 			         INT(5),\n"
"   	PRIMARY KEY (bioentry_relationship_id),\n"
"	UNIQUE (object_bioentry_id,subject_bioentry_id,term_id)\n"
") ENGINE=INNODB;\n"

"CREATE INDEX bioentryrel_trm   ON bioentry_relationship(term_id);\n"
"CREATE INDEX bioentryrel_child ON bioentry_relationship(subject_bioentry_id);\n"

"CREATE TABLE bioentry_path (\n"
"   	object_bioentry_id 	INT(10) UNSIGNED NOT NULL,\n"
"   	subject_bioentry_id 	INT(10) UNSIGNED NOT NULL,\n"
"   	term_id 		INT(10) UNSIGNED NOT NULL,\n"
"	distance	     	INT(10) UNSIGNED,\n"
"	UNIQUE (object_bioentry_id,subject_bioentry_id,term_id,distance)\n"
") ENGINE=INNODB;\n"

"CREATE INDEX bioentrypath_trm   ON bioentry_path(term_id);\n"
"CREATE INDEX bioentrypath_child ON bioentry_path(subject_bioentry_id);\n"

"CREATE TABLE biosequence (\n"
"  	bioentry_id     INT(10) UNSIGNED NOT NULL,\n"
"  	version     	SMALLINT, \n"
"  	length      	INT(10),\n"
"  	alphabet        VARCHAR(10),\n"
"  	seq 		LONGTEXT,\n"
"	PRIMARY KEY (bioentry_id)\n"
") ENGINE=INNODB;\n"

"CREATE TABLE dbxref (\n"
"        dbxref_id	INT(10) UNSIGNED NOT NULL auto_increment,\n"
"        dbname          VARCHAR(40) BINARY NOT NULL,\n"
"        accession       VARCHAR(128) BINARY NOT NULL,\n"
"	version		SMALLINT UNSIGNED NOT NULL,\n"
"	PRIMARY KEY (dbxref_id),\n"
"        UNIQUE(accession, dbname, version)\n"
") ENGINE=INNODB;\n"

"CREATE INDEX dbxref_db  ON dbxref(dbname);\n"

"CREATE TABLE dbxref_qualifier_value (\n"
"       	dbxref_id 		INT(10) UNSIGNED NOT NULL,\n"
"       	term_id 		INT(10) UNSIGNED NOT NULL,\n"
"  	rank  		   	SMALLINT NOT NULL DEFAULT 0,\n"
"       	value			TEXT,\n"
"	PRIMARY KEY (dbxref_id,term_id,rank)\n"
") ENGINE=INNODB;\n"

"CREATE INDEX dbxrefqual_dbx ON dbxref_qualifier_value(dbxref_id);\n"
"CREATE INDEX dbxrefqual_trm ON dbxref_qualifier_value(term_id);\n"

"CREATE TABLE bioentry_dbxref ( \n"
"       	bioentry_id        INT(10) UNSIGNED NOT NULL,\n"
"       	dbxref_id          INT(10) UNSIGNED NOT NULL,\n"
"  	rank  		   SMALLINT,\n"
"	PRIMARY KEY (bioentry_id,dbxref_id)\n"
") ENGINE=INNODB;\n"

"CREATE INDEX dblink_dbx  ON bioentry_dbxref(dbxref_id);\n"

"CREATE TABLE reference (\n"
"  	reference_id       INT(10) UNSIGNED NOT NULL auto_increment,\n"
"	dbxref_id	   INT(10) UNSIGNED,\n"
"  	location 	   TEXT NOT NULL,\n"
"  	title    	   TEXT,\n"
"  	authors  	   TEXT,\n"
"  	crc	   	   VARCHAR(32),\n"
"	PRIMARY KEY (reference_id),\n"
"	UNIQUE (dbxref_id),\n"
"	UNIQUE (crc)\n"
") ENGINE=INNODB;\n"

"CREATE TABLE bioentry_reference (\n"
"  	bioentry_id 	INT(10) UNSIGNED NOT NULL,\n"
"  	reference_id 	INT(10) UNSIGNED NOT NULL,\n"
"  	start_pos	INT(10),\n"
"  	end_pos	  	INT(10),\n"
"  	rank  		SMALLINT NOT NULL DEFAULT 0,\n"
"  	PRIMARY KEY(bioentry_id,reference_id,rank)\n"
") ENGINE=INNODB;\n"

"CREATE INDEX bioentryref_ref ON bioentry_reference(reference_id);\n"

"CREATE TABLE comment (\n"
"  	comment_id  	INT(10) UNSIGNED NOT NULL auto_increment,\n"
"  	bioentry_id    	INT(10) UNSIGNED NOT NULL,\n"
"  	comment_text   	TEXT NOT NULL,\n"
"  	rank   		SMALLINT NOT NULL DEFAULT 0,\n"
"	PRIMARY KEY (comment_id),\n"
"  	UNIQUE(bioentry_id, rank)\n"
") ENGINE=INNODB;\n"

"CREATE TABLE bioentry_qualifier_value (\n"
"	bioentry_id   		INT(10) UNSIGNED NOT NULL,\n"
"   	term_id  		INT(10) UNSIGNED NOT NULL,\n"
"   	value         		TEXT,\n"
"	rank			INT(5) NOT NULL DEFAULT 0,\n"
"	UNIQUE (bioentry_id,term_id,rank)\n"
") ENGINE=INNODB;\n"

"CREATE INDEX bioentryqual_trm ON bioentry_qualifier_value(term_id);\n"

"CREATE TABLE seqfeature (\n"
"   	seqfeature_id 		INT(10) UNSIGNED NOT NULL auto_increment,\n"
"   	bioentry_id   		INT(10) UNSIGNED NOT NULL,\n"
"   	type_term_id		INT(10) UNSIGNED NOT NULL,\n"
"   	source_term_id  	INT(10) UNSIGNED NOT NULL,\n"
"	display_name		VARCHAR(64),\n"
"   	rank 			SMALLINT UNSIGNED NOT NULL DEFAULT 0,\n"
"	PRIMARY KEY (seqfeature_id),\n"
"	UNIQUE (bioentry_id,type_term_id,source_term_id,rank)\n"
") ENGINE=INNODB;\n"

"CREATE INDEX seqfeature_trm  ON seqfeature(type_term_id);\n"
"CREATE INDEX seqfeature_fsrc ON seqfeature(source_term_id);\n"

"CREATE TABLE seqfeature_relationship (\n"
"        seqfeature_relationship_id INT(10) UNSIGNED NOT NULL auto_increment,\n"
"   	object_seqfeature_id	INT(10) UNSIGNED NOT NULL,\n"
"   	subject_seqfeature_id 	INT(10) UNSIGNED NOT NULL,\n"
"   	term_id 	        INT(10) UNSIGNED NOT NULL,\n"
"   	rank 			INT(5),\n"
"   	PRIMARY KEY (seqfeature_relationship_id),\n"
"	UNIQUE (object_seqfeature_id,subject_seqfeature_id,term_id)\n"
") ENGINE=INNODB;\n"

"CREATE INDEX seqfeaturerel_trm   ON seqfeature_relationship(term_id);\n"
"CREATE INDEX seqfeaturerel_child ON seqfeature_relationship(subject_seqfeature_id);\n"

"CREATE TABLE seqfeature_path (\n"
"   	object_seqfeature_id	INT(10) UNSIGNED NOT NULL,\n"
"   	subject_seqfeature_id 	INT(10) UNSIGNED NOT NULL,\n"
"   	term_id 		INT(10) UNSIGNED NOT NULL,\n"
"	distance	     	INT(10) UNSIGNED,\n"
"	UNIQUE (object_seqfeature_id,subject_seqfeature_id,term_id,distance)\n"
") ENGINE=INNODB;\n"

"CREATE INDEX seqfeaturepath_trm   ON seqfeature_path(term_id);\n"
"CREATE INDEX seqfeaturepath_child ON seqfeature_path(subject_seqfeature_id);\n"

"CREATE TABLE seqfeature_qualifier_value (\n"
"	seqfeature_id 		INT(10) UNSIGNED NOT NULL,\n"
"   	term_id 		INT(10) UNSIGNED NOT NULL,\n"
"   	rank 			SMALLINT NOT NULL DEFAULT 0,\n"
"   	value  			TEXT NOT NULL,\n"
"   	PRIMARY KEY (seqfeature_id,term_id,rank)\n"
") ENGINE=INNODB;\n"

"CREATE INDEX seqfeaturequal_trm ON seqfeature_qualifier_value(term_id);\n"
   
"CREATE TABLE seqfeature_dbxref ( \n"
"       	seqfeature_id      INT(10) UNSIGNED NOT NULL,\n"
"       	dbxref_id          INT(10) UNSIGNED NOT NULL,\n"
"  	rank  		   SMALLINT,\n"
"	PRIMARY KEY (seqfeature_id,dbxref_id)\n"
") ENGINE=INNODB;\n"

"CREATE INDEX feadblink_dbx  ON seqfeature_dbxref(dbxref_id);\n"

"CREATE TABLE location (\n"
"	location_id		INT(10) UNSIGNED NOT NULL auto_increment,\n"
"   	seqfeature_id		INT(10) UNSIGNED NOT NULL,\n"
"	dbxref_id		INT(10) UNSIGNED,\n"
"	term_id			INT(10) UNSIGNED,\n"
"   	start_pos              	INT(10),\n"
"   	end_pos                	INT(10),\n"
"   	strand             	TINYINT NOT NULL DEFAULT 0,\n"
"   	rank          		SMALLINT NOT NULL DEFAULT 0,\n"
"	PRIMARY KEY (location_id),\n"
"   	UNIQUE (seqfeature_id, rank)\n"
") ENGINE=INNODB;\n"

"CREATE INDEX seqfeatureloc_start ON location(start_pos, end_pos);\n"
"CREATE INDEX seqfeatureloc_dbx   ON location(dbxref_id);\n"
"CREATE INDEX seqfeatureloc_trm   ON location(term_id);\n"

"CREATE TABLE location_qualifier_value (\n"
"	location_id		INT(10) UNSIGNED NOT NULL,\n"
"   	term_id 		INT(10) UNSIGNED NOT NULL,\n"
"   	value  			VARCHAR(255) NOT NULL,\n"
"   	int_value 		INT(10),\n"
"	PRIMARY KEY (location_id,term_id)\n"
") ENGINE=INNODB;\n"

"CREATE INDEX locationqual_trm ON location_qualifier_value(term_id);\n"

"ALTER TABLE term ADD CONSTRAINT FKont_term\n"
"	FOREIGN KEY (ontology_id) REFERENCES ontology(ontology_id)\n"
"	ON DELETE CASCADE;\n"

"ALTER TABLE term_synonym ADD CONSTRAINT FKterm_syn\n"
"	FOREIGN KEY (term_id) REFERENCES term(term_id)\n"
"	ON DELETE CASCADE;\n"

"ALTER TABLE term_dbxref ADD CONSTRAINT FKdbxref_trmdbxref\n"
"       	FOREIGN KEY (dbxref_id) REFERENCES dbxref(dbxref_id)\n"
"	ON DELETE CASCADE;\n"
"ALTER TABLE term_dbxref ADD CONSTRAINT FKterm_trmdbxref\n"
"      FOREIGN KEY (term_id) REFERENCES term(term_id)\n"
"	ON DELETE CASCADE;\n"

"ALTER TABLE term_relationship ADD CONSTRAINT FKtrmsubject_trmrel\n"
"	FOREIGN KEY (subject_term_id) REFERENCES term(term_id)\n"
"	ON DELETE CASCADE;\n"
"ALTER TABLE term_relationship ADD CONSTRAINT FKtrmpredicate_trmrel\n"
"       	FOREIGN KEY (predicate_term_id) REFERENCES term(term_id)\n"
"	ON DELETE CASCADE;\n"
"ALTER TABLE term_relationship ADD CONSTRAINT FKtrmobject_trmrel\n"
"       	FOREIGN KEY (object_term_id) REFERENCES term(term_id)\n"
"	ON DELETE CASCADE;\n"
"ALTER TABLE term_relationship ADD CONSTRAINT FKterm_trmrel\n"
"       	FOREIGN KEY (ontology_id) REFERENCES ontology(ontology_id)\n"
"	ON DELETE CASCADE;\n"

"ALTER TABLE term_relationship_term ADD CONSTRAINT FKtrmrel_trmreltrm\n"
"	FOREIGN KEY (term_relationship_id) REFERENCES term_relationship(term_relationship_id)\n"
"	ON DELETE CASCADE;\n"
"ALTER TABLE term_relationship_term ADD CONSTRAINT FKtrm_trmreltrm\n"
"	FOREIGN KEY (term_id) REFERENCES term(term_id)\n"
"	ON DELETE CASCADE;\n"

"ALTER TABLE term_path ADD CONSTRAINT FKtrmsubject_trmpath\n"
"	FOREIGN KEY (subject_term_id) REFERENCES term(term_id)\n"
"	ON DELETE CASCADE;\n"
"ALTER TABLE term_path ADD CONSTRAINT FKtrmpredicate_trmpath\n"
"       	FOREIGN KEY (predicate_term_id) REFERENCES term(term_id)\n"
"	ON DELETE CASCADE;\n"
"ALTER TABLE term_path ADD CONSTRAINT FKtrmobject_trmpath\n"
"       	FOREIGN KEY (object_term_id) REFERENCES term(term_id)\n"
"	ON DELETE CASCADE;\n"
"ALTER TABLE term_path ADD CONSTRAINT FKontology_trmpath\n"
"       	FOREIGN KEY (ontology_id) REFERENCES ontology(ontology_id)\n"
"	ON DELETE CASCADE;\n"

"ALTER TABLE taxon_name ADD CONSTRAINT FKtaxon_taxonname\n"
"        FOREIGN KEY (taxon_id) REFERENCES taxon(taxon_id)\n"
"        ON DELETE CASCADE;\n"

"ALTER TABLE bioentry ADD CONSTRAINT FKtaxon_bioentry\n"
"	FOREIGN KEY (taxon_id) REFERENCES taxon(taxon_id);\n"
"ALTER TABLE bioentry ADD CONSTRAINT FKbiodatabase_bioentry\n"
"	FOREIGN KEY (biodatabase_id) REFERENCES biodatabase(biodatabase_id);\n"

"ALTER TABLE bioentry_relationship ADD CONSTRAINT FKterm_bioentryrel\n"
"	FOREIGN KEY (term_id) REFERENCES term(term_id);\n"
"ALTER TABLE bioentry_relationship ADD CONSTRAINT FKparentent_bioentryrel\n"
"	FOREIGN KEY (object_bioentry_id) REFERENCES bioentry(bioentry_id)\n"
"	ON DELETE CASCADE;\n"
"ALTER TABLE bioentry_relationship ADD CONSTRAINT FKchildent_bioentryrel\n"
"	FOREIGN KEY (subject_bioentry_id) REFERENCES bioentry(bioentry_id)\n"
"	ON DELETE CASCADE;\n"

"ALTER TABLE bioentry_path ADD CONSTRAINT FKterm_bioentrypath\n"
"	FOREIGN KEY (term_id) REFERENCES term(term_id);\n"
"ALTER TABLE bioentry_path ADD CONSTRAINT FKparentent_bioentrypath\n"
"	FOREIGN KEY (object_bioentry_id) REFERENCES bioentry(bioentry_id)\n"
"	ON DELETE CASCADE;\n"
"ALTER TABLE bioentry_path ADD CONSTRAINT FKchildent_bioentrypath\n"
"	FOREIGN KEY (subject_bioentry_id) REFERENCES bioentry(bioentry_id)\n"
"	ON DELETE CASCADE;\n"

"ALTER TABLE biosequence ADD CONSTRAINT FKbioentry_bioseq\n"
"	FOREIGN KEY (bioentry_id) REFERENCES bioentry(bioentry_id)\n"
"	ON DELETE CASCADE;\n"

"ALTER TABLE comment ADD CONSTRAINT FKbioentry_comment\n"
"	FOREIGN KEY(bioentry_id) REFERENCES bioentry(bioentry_id)\n"
"	ON DELETE CASCADE;\n"

"ALTER TABLE bioentry_dbxref ADD CONSTRAINT FKbioentry_dblink\n"
"        FOREIGN KEY (bioentry_id) REFERENCES bioentry(bioentry_id)\n"
"	ON DELETE CASCADE;\n"
"ALTER TABLE bioentry_dbxref ADD CONSTRAINT FKdbxref_dblink\n"
"       	FOREIGN KEY (dbxref_id) REFERENCES dbxref(dbxref_id)\n"
"	ON DELETE CASCADE;\n"

"ALTER TABLE dbxref_qualifier_value ADD CONSTRAINT FKtrm_dbxrefqual\n"
"	FOREIGN KEY (term_id) REFERENCES term(term_id);\n"
"ALTER TABLE dbxref_qualifier_value ADD CONSTRAINT FKdbxref_dbxrefqual\n"
"	FOREIGN KEY (dbxref_id) REFERENCES dbxref(dbxref_id)\n"
"	ON DELETE CASCADE;\n"

"ALTER TABLE bioentry_reference ADD CONSTRAINT FKbioentry_entryref\n"
"	FOREIGN KEY (bioentry_id) REFERENCES bioentry(bioentry_id)\n"
"	ON DELETE CASCADE;\n"
"ALTER TABLE bioentry_reference ADD CONSTRAINT FKreference_entryref\n"
"	FOREIGN KEY (reference_id) REFERENCES reference(reference_id)\n"
"	ON DELETE CASCADE;\n"

"ALTER TABLE bioentry_qualifier_value ADD CONSTRAINT FKbioentry_entqual\n"
"	FOREIGN KEY (bioentry_id) REFERENCES bioentry(bioentry_id)\n"
"	ON DELETE CASCADE;\n"
"ALTER TABLE bioentry_qualifier_value ADD CONSTRAINT FKterm_entqual\n"
"	FOREIGN KEY (term_id) REFERENCES term(term_id);\n"

"ALTER TABLE reference ADD CONSTRAINT FKdbxref_reference\n"
"      FOREIGN KEY ( dbxref_id ) REFERENCES dbxref ( dbxref_id ) ;\n"

"ALTER TABLE seqfeature ADD CONSTRAINT FKterm_seqfeature\n"
"	FOREIGN KEY (type_term_id) REFERENCES term(term_id);\n"
"ALTER TABLE seqfeature ADD CONSTRAINT FKsourceterm_seqfeature\n"
"	FOREIGN KEY (source_term_id) REFERENCES term(term_id);\n"
"ALTER TABLE seqfeature ADD CONSTRAINT FKbioentry_seqfeature\n"
"	FOREIGN KEY (bioentry_id) REFERENCES bioentry(bioentry_id)\n"
"	ON DELETE CASCADE;\n"

"ALTER TABLE seqfeature_relationship ADD CONSTRAINT FKterm_seqfeatrel\n"
"	FOREIGN KEY (term_id) REFERENCES term(term_id);\n"
"ALTER TABLE seqfeature_relationship ADD CONSTRAINT FKparentfeat_seqfeatrel\n"
"	FOREIGN KEY (object_seqfeature_id) REFERENCES seqfeature(seqfeature_id)\n"
"	ON DELETE CASCADE;\n"
"ALTER TABLE seqfeature_relationship ADD CONSTRAINT FKchildfeat_seqfeatrel\n"
"	FOREIGN KEY (subject_seqfeature_id) REFERENCES seqfeature(seqfeature_id)\n"
"	ON DELETE CASCADE;\n"

"ALTER TABLE seqfeature_path ADD CONSTRAINT FKterm_seqfeatpath\n"
"	FOREIGN KEY (term_id) REFERENCES term(term_id);\n"
"ALTER TABLE seqfeature_path ADD CONSTRAINT FKparentfeat_seqfeatpath\n"
"	FOREIGN KEY (object_seqfeature_id) REFERENCES seqfeature(seqfeature_id)\n"
"	ON DELETE CASCADE;\n"
"ALTER TABLE seqfeature_path ADD CONSTRAINT FKchildfeat_seqfeatpath\n"
"	FOREIGN KEY (subject_seqfeature_id) REFERENCES seqfeature(seqfeature_id)\n"
"	ON DELETE CASCADE;\n"

"ALTER TABLE seqfeature_qualifier_value ADD CONSTRAINT FKterm_featqual\n"
"	FOREIGN KEY (term_id) REFERENCES term(term_id);\n"
"ALTER TABLE seqfeature_qualifier_value ADD CONSTRAINT FKseqfeature_featqual\n"
"	FOREIGN KEY (seqfeature_id) REFERENCES seqfeature(seqfeature_id)\n"
"	ON DELETE CASCADE;\n"

"ALTER TABLE seqfeature_dbxref ADD CONSTRAINT FKseqfeature_feadblink\n"
"        FOREIGN KEY (seqfeature_id) REFERENCES seqfeature(seqfeature_id)\n"
"	ON DELETE CASCADE;\n"
"ALTER TABLE seqfeature_dbxref ADD CONSTRAINT FKdbxref_feadblink\n"
"       	FOREIGN KEY (dbxref_id) REFERENCES dbxref(dbxref_id)\n"
"	ON DELETE CASCADE;\n"

"ALTER TABLE location ADD CONSTRAINT FKseqfeature_location\n"
"	FOREIGN KEY (seqfeature_id) REFERENCES seqfeature(seqfeature_id)\n"
"	ON DELETE CASCADE;\n"
"ALTER TABLE location ADD CONSTRAINT FKdbxref_location\n"
"	FOREIGN KEY (dbxref_id) REFERENCES dbxref(dbxref_id);\n"
"ALTER TABLE location ADD CONSTRAINT FKterm_featloc\n"
"	FOREIGN KEY (term_id) REFERENCES term(term_id);\n"

"ALTER TABLE location_qualifier_value ADD CONSTRAINT FKfeatloc_locqual\n"
"	FOREIGN KEY (location_id) REFERENCES location(location_id)\n"
"	ON DELETE CASCADE;\n"
"ALTER TABLE location_qualifier_value ADD CONSTRAINT FKterm_locqual\n"
"	FOREIGN KEY (term_id) REFERENCES term(term_id);\n";

/**
**/
const char *c_my_sql_embedded_create_tables[] = {
"CREATE TABLE biodatabase (\n"
"  	biodatabase_id 	INT(10) UNSIGNED NOT NULL auto_increment,\n"
"  	name           	VARCHAR(128) BINARY NOT NULL,\n"
"	authority	VARCHAR(128),\n"
"	description	TEXT,\n"
"	PRIMARY KEY (biodatabase_id),\n"
"  	UNIQUE (name)\n"
") ENGINE=INNODB;",

"CREATE INDEX db_auth on biodatabase(authority);",

"CREATE TABLE taxon (\n"
"       taxon_id		INT(10) UNSIGNED NOT NULL auto_increment,\n"
"       ncbi_taxon_id 	INT(10),\n"
"       parent_taxon_id	INT(10) UNSIGNED,\n"
"       node_rank	VARCHAR(32),\n"
"       genetic_code	TINYINT UNSIGNED,\n"
"       mito_genetic_code TINYINT UNSIGNED,\n"
"       left_value	INT(10) UNSIGNED,\n"
"       right_value	INT(10) UNSIGNED,\n"
"       PRIMARY KEY (taxon_id),\n"
"       UNIQUE (ncbi_taxon_id),\n"
"       UNIQUE (left_value),\n"
"       UNIQUE (right_value)\n"
") ENGINE=INNODB;",

"CREATE INDEX taxparent ON taxon(parent_taxon_id);",

"CREATE TABLE taxon_name (\n"
"       taxon_id		INT(10) UNSIGNED NOT NULL,\n"
"       name		VARCHAR(255) BINARY NOT NULL,\n"
"       name_class	VARCHAR(32) BINARY NOT NULL,\n"
"       UNIQUE (taxon_id,name,name_class)\n"
") ENGINE=INNODB;",

"CREATE INDEX taxnametaxonid ON taxon_name(taxon_id);",
"CREATE INDEX taxnamename    ON taxon_name(name);",

"CREATE TABLE ontology (\n"
"       	ontology_id        INT(10) UNSIGNED NOT NULL auto_increment,\n"
"       	name	   	   VARCHAR(32) BINARY NOT NULL,\n"
"       	definition	   TEXT,\n"
"	PRIMARY KEY (ontology_id),\n"
"	UNIQUE (name)\n"
") ENGINE=INNODB;",

"CREATE TABLE term (\n"
"       	term_id   INT(10) UNSIGNED NOT NULL auto_increment,\n"
"       	name	   	   VARCHAR(255) BINARY NOT NULL,\n"
"       	definition	   TEXT,\n"
"	identifier	   VARCHAR(40) BINARY,\n"
"	is_obsolete	   CHAR(1),\n"
"	ontology_id	   INT(10) UNSIGNED NOT NULL,\n"
"	PRIMARY KEY (term_id),\n"
"	UNIQUE (identifier),\n"
"          UNIQUE (name,ontology_id,is_obsolete)\n"
") ENGINE=INNODB;",

"CREATE INDEX term_ont ON term(ontology_id);",

"CREATE TABLE term_synonym (\n"
"       synonym		  VARCHAR(255) BINARY NOT NULL,\n"
"       term_id		  INT(10) UNSIGNED NOT NULL,\n"
"       PRIMARY KEY (term_id,synonym)\n"
") ENGINE=INNODB;",

"CREATE TABLE term_dbxref (\n"
"       	term_id	          INT(10) UNSIGNED NOT NULL,\n"
"       	dbxref_id         INT(10) UNSIGNED NOT NULL,\n"
"	rank		  SMALLINT,\n"
"	PRIMARY KEY (term_id, dbxref_id)\n"
") ENGINE=INNODB;",

"CREATE INDEX trmdbxref_dbxrefid ON term_dbxref(dbxref_id);",

"CREATE TABLE term_relationship (\n"
"        term_relationship_id INT(10) UNSIGNED NOT NULL auto_increment,\n"
"       	subject_term_id	INT(10) UNSIGNED NOT NULL,\n"
"       	predicate_term_id    INT(10) UNSIGNED NOT NULL,\n"
"       	object_term_id       INT(10) UNSIGNED NOT NULL,\n"
"	ontology_id	INT(10) UNSIGNED NOT NULL,\n"
"	PRIMARY KEY (term_relationship_id),\n"
"	UNIQUE (subject_term_id,predicate_term_id,object_term_id,ontology_id)\n"
") ENGINE=INNODB;",

"CREATE INDEX trmrel_predicateid ON term_relationship(predicate_term_id);",
"CREATE INDEX trmrel_objectid ON term_relationship(object_term_id);",
"CREATE INDEX trmrel_ontid ON term_relationship(ontology_id);",

"CREATE TABLE term_relationship_term (\n"
"        term_relationship_id INT(10) UNSIGNED NOT NULL,\n"
"        term_id              INT(10) UNSIGNED NOT NULL,\n"
"        PRIMARY KEY ( term_relationship_id ),\n"
"        UNIQUE ( term_id ) \n"
") ENGINE=INNODB;",

"CREATE TABLE term_path (\n"
"        term_path_id         INT(10) UNSIGNED NOT NULL auto_increment,\n"
"       	subject_term_id	     INT(10) UNSIGNED NOT NULL,\n"
"       	predicate_term_id    INT(10) UNSIGNED NOT NULL,\n"
"       	object_term_id       INT(10) UNSIGNED NOT NULL,\n"
"	ontology_id          INT(10) UNSIGNED NOT NULL,\n"
"	distance	     INT(10) UNSIGNED,\n"
"	PRIMARY KEY (term_path_id),\n"
"	UNIQUE (subject_term_id,predicate_term_id,object_term_id,ontology_id,distance)\n"
") ENGINE=INNODB;",

"CREATE INDEX trmpath_predicateid ON term_path(predicate_term_id);",
"CREATE INDEX trmpath_objectid ON term_path(object_term_id);",
"CREATE INDEX trmpath_ontid ON term_path(ontology_id);",

"CREATE TABLE bioentry (\n"
"	bioentry_id	    INT(10) UNSIGNED NOT NULL auto_increment,\n"
"  	biodatabase_id  INT(10) UNSIGNED NOT NULL,\n"
"  	taxon_id     	INT(10) UNSIGNED,\n"
"  	name		VARCHAR(40) NOT NULL,\n"
"  	accession    	VARCHAR(128) BINARY NOT NULL,\n"
"  	identifier   	VARCHAR(40) BINARY,\n"
"	division	VARCHAR(6),\n"
"  	description  	TEXT,\n"
"  	version 	SMALLINT UNSIGNED NOT NULL, \n"
"	PRIMARY KEY (bioentry_id),\n"
"  	UNIQUE (accession,biodatabase_id,version),\n"
" 	UNIQUE (identifier, biodatabase_id)\n"
") ENGINE=INNODB;",

"CREATE INDEX bioentry_name ON bioentry(name);",
"CREATE INDEX bioentry_db   ON bioentry(biodatabase_id);",
"CREATE INDEX bioentry_tax  ON bioentry(taxon_id);",

"CREATE TABLE bioentry_relationship (\n"
"    bioentry_relationship_id INT(10) UNSIGNED NOT NULL auto_increment,\n"
"    object_bioentry_id 	     INT(10) UNSIGNED NOT NULL,\n"
"   	subject_bioentry_id 	 INT(10) UNSIGNED NOT NULL,\n"
"   	term_id 		         INT(10) UNSIGNED NOT NULL,\n"
"   	rank 			         INT(5),\n"
"   	PRIMARY KEY (bioentry_relationship_id),\n"
"	UNIQUE (object_bioentry_id,subject_bioentry_id,term_id)\n"
") ENGINE=INNODB;",

"CREATE INDEX bioentryrel_trm   ON bioentry_relationship(term_id);",
"CREATE INDEX bioentryrel_child ON bioentry_relationship(subject_bioentry_id);",

"CREATE TABLE bioentry_path (\n"
"   	object_bioentry_id 	INT(10) UNSIGNED NOT NULL,\n"
"   	subject_bioentry_id 	INT(10) UNSIGNED NOT NULL,\n"
"   	term_id 		INT(10) UNSIGNED NOT NULL,\n"
"	distance	     	INT(10) UNSIGNED,\n"
"	UNIQUE (object_bioentry_id,subject_bioentry_id,term_id,distance)\n"
") ENGINE=INNODB;",

"CREATE INDEX bioentrypath_trm   ON bioentry_path(term_id);",
"CREATE INDEX bioentrypath_child ON bioentry_path(subject_bioentry_id);",

"CREATE TABLE biosequence (\n"
"  	bioentry_id     INT(10) UNSIGNED NOT NULL,\n"
"  	version     	SMALLINT, \n"
"  	length      	INT(10),\n"
"  	alphabet        VARCHAR(10),\n"
"  	seq 		LONGTEXT,\n"
"	PRIMARY KEY (bioentry_id)\n"
") ENGINE=INNODB;",

"CREATE TABLE dbxref (\n"
"        dbxref_id	INT(10) UNSIGNED NOT NULL auto_increment,\n"
"        dbname          VARCHAR(40) BINARY NOT NULL,\n"
"        accession       VARCHAR(128) BINARY NOT NULL,\n"
"	version		SMALLINT UNSIGNED NOT NULL,\n"
"	PRIMARY KEY (dbxref_id),\n"
"        UNIQUE(accession, dbname, version)\n"
") ENGINE=INNODB;",

"CREATE INDEX dbxref_db  ON dbxref(dbname);",

"CREATE TABLE dbxref_qualifier_value (\n"
"       	dbxref_id 		INT(10) UNSIGNED NOT NULL,\n"
"       	term_id 		INT(10) UNSIGNED NOT NULL,\n"
"  	rank  		   	SMALLINT NOT NULL DEFAULT 0,\n"
"       	value			TEXT,\n"
"	PRIMARY KEY (dbxref_id,term_id,rank)\n"
") ENGINE=INNODB;",

"CREATE INDEX dbxrefqual_dbx ON dbxref_qualifier_value(dbxref_id);",
"CREATE INDEX dbxrefqual_trm ON dbxref_qualifier_value(term_id);",

"CREATE TABLE bioentry_dbxref ( \n"
"       	bioentry_id        INT(10) UNSIGNED NOT NULL,\n"
"       	dbxref_id          INT(10) UNSIGNED NOT NULL,\n"
"  	rank  		   SMALLINT,\n"
"	PRIMARY KEY (bioentry_id,dbxref_id)\n"
") ENGINE=INNODB;",

"CREATE INDEX dblink_dbx  ON bioentry_dbxref(dbxref_id);",

"CREATE TABLE reference (\n"
"  	reference_id       INT(10) UNSIGNED NOT NULL auto_increment,\n"
"	dbxref_id	   INT(10) UNSIGNED,\n"
"  	location 	   TEXT NOT NULL,\n"
"  	title    	   TEXT,\n"
"  	authors  	   TEXT,\n"
"  	crc	   	   VARCHAR(32),\n"
"	PRIMARY KEY (reference_id),\n"
"	UNIQUE (dbxref_id),\n"
"	UNIQUE (crc)\n"
") ENGINE=INNODB;",

"CREATE TABLE bioentry_reference (\n"
"  	bioentry_id 	INT(10) UNSIGNED NOT NULL,\n"
"  	reference_id 	INT(10) UNSIGNED NOT NULL,\n"
"  	start_pos	INT(10),\n"
"  	end_pos	  	INT(10),\n"
"  	rank  		SMALLINT NOT NULL DEFAULT 0,\n"
"  	PRIMARY KEY(bioentry_id,reference_id,rank)\n"
") ENGINE=INNODB;",

"CREATE INDEX bioentryref_ref ON bioentry_reference(reference_id);",

"CREATE TABLE comment (\n"
"  	comment_id  	INT(10) UNSIGNED NOT NULL auto_increment,\n"
"  	bioentry_id    	INT(10) UNSIGNED NOT NULL,\n"
"  	comment_text   	TEXT NOT NULL,\n"
"  	rank   		SMALLINT NOT NULL DEFAULT 0,\n"
"	PRIMARY KEY (comment_id),\n"
"  	UNIQUE(bioentry_id, rank)\n"
") ENGINE=INNODB;",

"CREATE TABLE bioentry_qualifier_value (\n"
"	bioentry_id   		INT(10) UNSIGNED NOT NULL,\n"
"   	term_id  		INT(10) UNSIGNED NOT NULL,\n"
"   	value         		TEXT,\n"
"	rank			INT(5) NOT NULL DEFAULT 0,\n"
"	UNIQUE (bioentry_id,term_id,rank)\n"
") ENGINE=INNODB;",

"CREATE INDEX bioentryqual_trm ON bioentry_qualifier_value(term_id);",

"CREATE TABLE seqfeature (\n"
"   	seqfeature_id 		INT(10) UNSIGNED NOT NULL auto_increment,\n"
"   	bioentry_id   		INT(10) UNSIGNED NOT NULL,\n"
"   	type_term_id		INT(10) UNSIGNED NOT NULL,\n"
"   	source_term_id  	INT(10) UNSIGNED NOT NULL,\n"
"	display_name		VARCHAR(64),\n"
"   	rank 			SMALLINT UNSIGNED NOT NULL DEFAULT 0,\n"
"	PRIMARY KEY (seqfeature_id),\n"
"	UNIQUE (bioentry_id,type_term_id,source_term_id,rank)\n"
") ENGINE=INNODB;",

"CREATE INDEX seqfeature_trm  ON seqfeature(type_term_id);",
"CREATE INDEX seqfeature_fsrc ON seqfeature(source_term_id);",

"CREATE TABLE seqfeature_relationship (\n"
"        seqfeature_relationship_id INT(10) UNSIGNED NOT NULL auto_increment,\n"
"   	object_seqfeature_id	INT(10) UNSIGNED NOT NULL,\n"
"   	subject_seqfeature_id 	INT(10) UNSIGNED NOT NULL,\n"
"   	term_id 	        INT(10) UNSIGNED NOT NULL,\n"
"   	rank 			INT(5),\n"
"   	PRIMARY KEY (seqfeature_relationship_id),\n"
"	UNIQUE (object_seqfeature_id,subject_seqfeature_id,term_id)\n"
") ENGINE=INNODB;",

"CREATE INDEX seqfeaturerel_trm   ON seqfeature_relationship(term_id);",
"CREATE INDEX seqfeaturerel_child ON seqfeature_relationship(subject_seqfeature_id);",

"CREATE TABLE seqfeature_path (\n"
"   	object_seqfeature_id	INT(10) UNSIGNED NOT NULL,\n"
"   	subject_seqfeature_id 	INT(10) UNSIGNED NOT NULL,\n"
"   	term_id 		INT(10) UNSIGNED NOT NULL,\n"
"	distance	     	INT(10) UNSIGNED,\n"
"	UNIQUE (object_seqfeature_id,subject_seqfeature_id,term_id,distance)\n"
") ENGINE=INNODB;",

"CREATE INDEX seqfeaturepath_trm   ON seqfeature_path(term_id);",
"CREATE INDEX seqfeaturepath_child ON seqfeature_path(subject_seqfeature_id);",

"CREATE TABLE seqfeature_qualifier_value (\n"
"	seqfeature_id 		INT(10) UNSIGNED NOT NULL,\n"
"   	term_id 		INT(10) UNSIGNED NOT NULL,\n"
"   	rank 			SMALLINT NOT NULL DEFAULT 0,\n"
"   	value  			TEXT NOT NULL,\n"
"   	PRIMARY KEY (seqfeature_id,term_id,rank)\n"
") ENGINE=INNODB;",

"CREATE INDEX seqfeaturequal_trm ON seqfeature_qualifier_value(term_id);",
   
"CREATE TABLE seqfeature_dbxref ( \n"
"       	seqfeature_id      INT(10) UNSIGNED NOT NULL,\n"
"       	dbxref_id          INT(10) UNSIGNED NOT NULL,\n"
"  	rank  		   SMALLINT,\n"
"	PRIMARY KEY (seqfeature_id,dbxref_id)\n"
") ENGINE=INNODB;",

"CREATE INDEX feadblink_dbx  ON seqfeature_dbxref(dbxref_id);",

"CREATE TABLE location (\n"
"	location_id		INT(10) UNSIGNED NOT NULL auto_increment,\n"
"   	seqfeature_id		INT(10) UNSIGNED NOT NULL,\n"
"	dbxref_id		INT(10) UNSIGNED,\n"
"	term_id			INT(10) UNSIGNED,\n"
"   	start_pos              	INT(10),\n"
"   	end_pos                	INT(10),\n"
"   	strand             	TINYINT NOT NULL DEFAULT 0,\n"
"   	rank          		SMALLINT NOT NULL DEFAULT 0,\n"
"	PRIMARY KEY (location_id),\n"
"   	UNIQUE (seqfeature_id, rank)\n"
") ENGINE=INNODB;",

"CREATE INDEX seqfeatureloc_start ON location(start_pos, end_pos);",
"CREATE INDEX seqfeatureloc_dbx   ON location(dbxref_id);",
"CREATE INDEX seqfeatureloc_trm   ON location(term_id);",

"CREATE TABLE location_qualifier_value (\n"
"	location_id		INT(10) UNSIGNED NOT NULL,\n"
"   	term_id 		INT(10) UNSIGNED NOT NULL,\n"
"   	value  			VARCHAR(255) NOT NULL,\n"
"   	int_value 		INT(10),\n"
"	PRIMARY KEY (location_id,term_id)\n"
") ENGINE=INNODB;",

"CREATE INDEX locationqual_trm ON location_qualifier_value(term_id);",

"ALTER TABLE term ADD CONSTRAINT FKont_term\n"
"	FOREIGN KEY (ontology_id) REFERENCES ontology(ontology_id)\n"
"	ON DELETE CASCADE;",

"ALTER TABLE term_synonym ADD CONSTRAINT FKterm_syn\n"
"	FOREIGN KEY (term_id) REFERENCES term(term_id)\n"
"	ON DELETE CASCADE;",

"ALTER TABLE term_dbxref ADD CONSTRAINT FKdbxref_trmdbxref\n"
"       	FOREIGN KEY (dbxref_id) REFERENCES dbxref(dbxref_id)\n"
"	ON DELETE CASCADE;",
"ALTER TABLE term_dbxref ADD CONSTRAINT FKterm_trmdbxref\n"
"      FOREIGN KEY (term_id) REFERENCES term(term_id)\n"
"	ON DELETE CASCADE;",

"ALTER TABLE term_relationship ADD CONSTRAINT FKtrmsubject_trmrel\n"
"	FOREIGN KEY (subject_term_id) REFERENCES term(term_id)\n"
"	ON DELETE CASCADE;",
"ALTER TABLE term_relationship ADD CONSTRAINT FKtrmpredicate_trmrel\n"
"       	FOREIGN KEY (predicate_term_id) REFERENCES term(term_id)\n"
"	ON DELETE CASCADE;",
"ALTER TABLE term_relationship ADD CONSTRAINT FKtrmobject_trmrel\n"
"       	FOREIGN KEY (object_term_id) REFERENCES term(term_id)\n"
"	ON DELETE CASCADE;",
"ALTER TABLE term_relationship ADD CONSTRAINT FKterm_trmrel\n"
"       	FOREIGN KEY (ontology_id) REFERENCES ontology(ontology_id)\n"
"	ON DELETE CASCADE;",

"ALTER TABLE term_relationship_term ADD CONSTRAINT FKtrmrel_trmreltrm\n"
"	FOREIGN KEY (term_relationship_id) REFERENCES term_relationship(term_relationship_id)\n"
"	ON DELETE CASCADE;",
"ALTER TABLE term_relationship_term ADD CONSTRAINT FKtrm_trmreltrm\n"
"	FOREIGN KEY (term_id) REFERENCES term(term_id)\n"
"	ON DELETE CASCADE;",

"ALTER TABLE term_path ADD CONSTRAINT FKtrmsubject_trmpath\n"
"	FOREIGN KEY (subject_term_id) REFERENCES term(term_id)\n"
"	ON DELETE CASCADE;",
"ALTER TABLE term_path ADD CONSTRAINT FKtrmpredicate_trmpath\n"
"       	FOREIGN KEY (predicate_term_id) REFERENCES term(term_id)\n"
"	ON DELETE CASCADE;",
"ALTER TABLE term_path ADD CONSTRAINT FKtrmobject_trmpath\n"
"       	FOREIGN KEY (object_term_id) REFERENCES term(term_id)\n"
"	ON DELETE CASCADE;",
"ALTER TABLE term_path ADD CONSTRAINT FKontology_trmpath\n"
"       	FOREIGN KEY (ontology_id) REFERENCES ontology(ontology_id)\n"
"	ON DELETE CASCADE;",

"ALTER TABLE taxon_name ADD CONSTRAINT FKtaxon_taxonname\n"
"        FOREIGN KEY (taxon_id) REFERENCES taxon(taxon_id)\n"
"        ON DELETE CASCADE;",

"ALTER TABLE bioentry ADD CONSTRAINT FKtaxon_bioentry\n"
"	FOREIGN KEY (taxon_id) REFERENCES taxon(taxon_id);",
"ALTER TABLE bioentry ADD CONSTRAINT FKbiodatabase_bioentry\n"
"	FOREIGN KEY (biodatabase_id) REFERENCES biodatabase(biodatabase_id);",

"ALTER TABLE bioentry_relationship ADD CONSTRAINT FKterm_bioentryrel\n"
"	FOREIGN KEY (term_id) REFERENCES term(term_id);",
"ALTER TABLE bioentry_relationship ADD CONSTRAINT FKparentent_bioentryrel\n"
"	FOREIGN KEY (object_bioentry_id) REFERENCES bioentry(bioentry_id)\n"
"	ON DELETE CASCADE;",
"ALTER TABLE bioentry_relationship ADD CONSTRAINT FKchildent_bioentryrel\n"
"	FOREIGN KEY (subject_bioentry_id) REFERENCES bioentry(bioentry_id)\n"
"	ON DELETE CASCADE;",

"ALTER TABLE bioentry_path ADD CONSTRAINT FKterm_bioentrypath\n"
"	FOREIGN KEY (term_id) REFERENCES term(term_id);",
"ALTER TABLE bioentry_path ADD CONSTRAINT FKparentent_bioentrypath\n"
"	FOREIGN KEY (object_bioentry_id) REFERENCES bioentry(bioentry_id)\n"
"	ON DELETE CASCADE;",
"ALTER TABLE bioentry_path ADD CONSTRAINT FKchildent_bioentrypath\n"
"	FOREIGN KEY (subject_bioentry_id) REFERENCES bioentry(bioentry_id)\n"
"	ON DELETE CASCADE;",

"ALTER TABLE biosequence ADD CONSTRAINT FKbioentry_bioseq\n"
"	FOREIGN KEY (bioentry_id) REFERENCES bioentry(bioentry_id)\n"
"	ON DELETE CASCADE;",

"ALTER TABLE comment ADD CONSTRAINT FKbioentry_comment\n"
"	FOREIGN KEY(bioentry_id) REFERENCES bioentry(bioentry_id)\n"
"	ON DELETE CASCADE;",

"ALTER TABLE bioentry_dbxref ADD CONSTRAINT FKbioentry_dblink\n"
"        FOREIGN KEY (bioentry_id) REFERENCES bioentry(bioentry_id)\n"
"	ON DELETE CASCADE;",
"ALTER TABLE bioentry_dbxref ADD CONSTRAINT FKdbxref_dblink\n"
"       	FOREIGN KEY (dbxref_id) REFERENCES dbxref(dbxref_id)\n"
"	ON DELETE CASCADE;",

"ALTER TABLE dbxref_qualifier_value ADD CONSTRAINT FKtrm_dbxrefqual\n"
"	FOREIGN KEY (term_id) REFERENCES term(term_id);",
"ALTER TABLE dbxref_qualifier_value ADD CONSTRAINT FKdbxref_dbxrefqual\n"
"	FOREIGN KEY (dbxref_id) REFERENCES dbxref(dbxref_id)\n"
"	ON DELETE CASCADE;",

"ALTER TABLE bioentry_reference ADD CONSTRAINT FKbioentry_entryref\n"
"	FOREIGN KEY (bioentry_id) REFERENCES bioentry(bioentry_id)\n"
"	ON DELETE CASCADE;",
"ALTER TABLE bioentry_reference ADD CONSTRAINT FKreference_entryref\n"
"	FOREIGN KEY (reference_id) REFERENCES reference(reference_id)\n"
"	ON DELETE CASCADE;",

"ALTER TABLE bioentry_qualifier_value ADD CONSTRAINT FKbioentry_entqual\n"
"	FOREIGN KEY (bioentry_id) REFERENCES bioentry(bioentry_id)\n"
"	ON DELETE CASCADE;",
"ALTER TABLE bioentry_qualifier_value ADD CONSTRAINT FKterm_entqual\n"
"	FOREIGN KEY (term_id) REFERENCES term(term_id);",

"ALTER TABLE reference ADD CONSTRAINT FKdbxref_reference\n"
"      FOREIGN KEY ( dbxref_id ) REFERENCES dbxref ( dbxref_id ) ;",

"ALTER TABLE seqfeature ADD CONSTRAINT FKterm_seqfeature\n"
"	FOREIGN KEY (type_term_id) REFERENCES term(term_id);",
"ALTER TABLE seqfeature ADD CONSTRAINT FKsourceterm_seqfeature\n"
"	FOREIGN KEY (source_term_id) REFERENCES term(term_id);",
"ALTER TABLE seqfeature ADD CONSTRAINT FKbioentry_seqfeature\n"
"	FOREIGN KEY (bioentry_id) REFERENCES bioentry(bioentry_id)\n"
"	ON DELETE CASCADE;",

"ALTER TABLE seqfeature_relationship ADD CONSTRAINT FKterm_seqfeatrel\n"
"	FOREIGN KEY (term_id) REFERENCES term(term_id);",
"ALTER TABLE seqfeature_relationship ADD CONSTRAINT FKparentfeat_seqfeatrel\n"
"	FOREIGN KEY (object_seqfeature_id) REFERENCES seqfeature(seqfeature_id)\n"
"	ON DELETE CASCADE;",
"ALTER TABLE seqfeature_relationship ADD CONSTRAINT FKchildfeat_seqfeatrel\n"
"	FOREIGN KEY (subject_seqfeature_id) REFERENCES seqfeature(seqfeature_id)\n"
"	ON DELETE CASCADE;",

"ALTER TABLE seqfeature_path ADD CONSTRAINT FKterm_seqfeatpath\n"
"	FOREIGN KEY (term_id) REFERENCES term(term_id);",
"ALTER TABLE seqfeature_path ADD CONSTRAINT FKparentfeat_seqfeatpath\n"
"	FOREIGN KEY (object_seqfeature_id) REFERENCES seqfeature(seqfeature_id)\n"
"	ON DELETE CASCADE;",
"ALTER TABLE seqfeature_path ADD CONSTRAINT FKchildfeat_seqfeatpath\n"
"	FOREIGN KEY (subject_seqfeature_id) REFERENCES seqfeature(seqfeature_id)\n"
"	ON DELETE CASCADE;",

"ALTER TABLE seqfeature_qualifier_value ADD CONSTRAINT FKterm_featqual\n"
"	FOREIGN KEY (term_id) REFERENCES term(term_id);",
"ALTER TABLE seqfeature_qualifier_value ADD CONSTRAINT FKseqfeature_featqual\n"
"	FOREIGN KEY (seqfeature_id) REFERENCES seqfeature(seqfeature_id)\n"
"	ON DELETE CASCADE;",

"ALTER TABLE seqfeature_dbxref ADD CONSTRAINT FKseqfeature_feadblink\n"
"        FOREIGN KEY (seqfeature_id) REFERENCES seqfeature(seqfeature_id)\n"
"	ON DELETE CASCADE;",
"ALTER TABLE seqfeature_dbxref ADD CONSTRAINT FKdbxref_feadblink\n"
"       	FOREIGN KEY (dbxref_id) REFERENCES dbxref(dbxref_id)\n"
"	ON DELETE CASCADE;",

"ALTER TABLE location ADD CONSTRAINT FKseqfeature_location\n"
"	FOREIGN KEY (seqfeature_id) REFERENCES seqfeature(seqfeature_id)\n"
"	ON DELETE CASCADE;",
"ALTER TABLE location ADD CONSTRAINT FKdbxref_location\n"
"	FOREIGN KEY (dbxref_id) REFERENCES dbxref(dbxref_id);",
"ALTER TABLE location ADD CONSTRAINT FKterm_featloc\n"
"	FOREIGN KEY (term_id) REFERENCES term(term_id);",

"ALTER TABLE location_qualifier_value ADD CONSTRAINT FKfeatloc_locqual\n"
"	FOREIGN KEY (location_id) REFERENCES location(location_id)\n"
"	ON DELETE CASCADE;",
"ALTER TABLE location_qualifier_value ADD CONSTRAINT FKterm_locqual\n"
"	FOREIGN KEY (term_id) REFERENCES term(term_id);",
0 
};

/**
**/
const char *c_postgre_create_tables =
"CREATE SEQUENCE biodatabase_pk_seq;\n"
"CREATE TABLE biodatabase ( \n"
"	 biodatabase_id INTEGER DEFAULT nextval ( 'biodatabase_pk_seq' ) NOT NULL , \n"
"	 name VARCHAR ( 128 ) NOT NULL , \n"
"	 authority VARCHAR ( 128 ) , \n"
"	 description TEXT , \n"
"	 PRIMARY KEY ( biodatabase_id ) , \n"
"	 UNIQUE ( name ) ) ; \n"

"CREATE INDEX db_auth on biodatabase ( authority ); \n"

"CREATE SEQUENCE taxon_pk_seq;\n"
"CREATE TABLE taxon ( \n"
"	 taxon_id INTEGER DEFAULT nextval ( 'taxon_pk_seq' ) NOT NULL , \n"
"	 ncbi_taxon_id INTEGER , \n"
"	 parent_taxon_id INTEGER , \n"
"	 node_rank VARCHAR ( 32 ) , \n"
"	 genetic_code SMALLINT , \n"
"	 mito_genetic_code SMALLINT , \n"
"	 left_value INTEGER , \n"
"	 right_value INTEGER , \n"
"	 PRIMARY KEY ( taxon_id ) , \n"
"	 CONSTRAINT XAKtaxon_ncbi_taxon_id UNIQUE ( ncbi_taxon_id ) , \n"
"	 CONSTRAINT XAKtaxon_left_value UNIQUE ( left_value ) , \n"
"	 CONSTRAINT XAKtaxon_right_value UNIQUE ( right_value ) ) ; \n"

"CREATE INDEX taxparent ON taxon ( parent_taxon_id ); \n"

"CREATE TABLE taxon_name ( \n"
"	 taxon_id INTEGER NOT NULL , \n"
"	 name VARCHAR ( 255 ) NOT NULL , \n"
"	 name_class VARCHAR ( 32 ) NOT NULL , \n"
"	 UNIQUE ( name , name_class, taxon_id ) ) ; \n"

"CREATE INDEX taxnametaxonid ON taxon_name ( taxon_id ); \n"
"CREATE INDEX taxnamename ON taxon_name ( name ); \n"

"CREATE SEQUENCE ontology_pk_seq;\n"
"CREATE TABLE ontology ( \n"
"	 ontology_id INTEGER DEFAULT nextval ( 'ontology_pk_seq' ) NOT NULL , \n"
"	 name VARCHAR ( 32 ) NOT NULL , \n"
"	 definition TEXT , \n"
"	 PRIMARY KEY ( ontology_id ) , \n"
"	 UNIQUE ( name ) ) ; \n"

"CREATE SEQUENCE term_pk_seq;\n"
"CREATE TABLE term ( \n"
"	 term_id INTEGER DEFAULT nextval ( 'term_pk_seq' ) NOT NULL , \n"
"	 name VARCHAR ( 255 ) NOT NULL , \n"
"	 definition TEXT , \n"
"	 identifier VARCHAR ( 40 ) , \n"
"	 is_obsolete CHAR ( 1 ) ,\n"
"	 ontology_id INTEGER NOT NULL , \n"
"	 PRIMARY KEY ( term_id ) , \n"
"       UNIQUE ( name , ontology_id , is_obsolete ) , \n"
"       UNIQUE ( identifier ) ) ; \n"

"CREATE INDEX term_ont ON term ( ontology_id ); \n"

"CREATE TABLE term_synonym (\n"
"	 synonym VARCHAR(255) NOT NULL,\n"
"	 term_id INTEGER NOT NULL,\n"
"       	 PRIMARY KEY ( term_id , synonym ) ) ;\n"

"CREATE TABLE term_dbxref ( \n"
"	 term_id INTEGER NOT NULL , \n"
"	 dbxref_id INTEGER NOT NULL , \n"
"	 rank INTEGER , \n"
"	 PRIMARY KEY ( term_id , dbxref_id ) ) ; \n"

"CREATE INDEX trmdbxref_dbxrefid ON term_dbxref ( dbxref_id ); \n"

"CREATE SEQUENCE term_relationship_pk_seq;\n"
"CREATE TABLE term_relationship ( \n"
"	 term_relationship_id INTEGER DEFAULT nextval ( 'term_relationship_pk_seq' ) NOT NULL , \n"
"	 subject_term_id INTEGER NOT NULL , \n"
"	 predicate_term_id INTEGER NOT NULL , \n"
"	 object_term_id INTEGER NOT NULL , \n"
"	 ontology_id INTEGER NOT NULL , \n"
"	 PRIMARY KEY ( term_relationship_id ) , \n"
"	 UNIQUE ( subject_term_id , predicate_term_id , object_term_id , ontology_id ) ) ; \n"

"CREATE INDEX trmrel_predicateid ON term_relationship ( predicate_term_id ); \n"
"CREATE INDEX trmrel_objectid ON term_relationship ( object_term_id ); \n"
"CREATE INDEX trmrel_ontid ON term_relationship ( ontology_id ); \n"

"CREATE TABLE term_relationship_term (\n"
"        term_relationship_id INTEGER NOT NULL,\n"
"        term_id              INTEGER NOT NULL,\n"
"        PRIMARY KEY ( term_relationship_id ),\n"
"        UNIQUE ( term_id ) \n"
");\n"

"CREATE SEQUENCE term_path_pk_seq;\n"
"CREATE TABLE term_path ( \n"
"         term_path_id INTEGER DEFAULT nextval ( 'term_path_pk_seq' ) NOT NULL ,\n"
"	 subject_term_id INTEGER NOT NULL , \n"
"	 predicate_term_id INTEGER NOT NULL , \n"
"	 object_term_id INTEGER NOT NULL , \n"
"	 ontology_id INTEGER NOT NULL , \n"
"	 distance INTEGER , \n"
"	 PRIMARY KEY (term_path_id),\n"
"	 UNIQUE ( subject_term_id , predicate_term_id , object_term_id , ontology_id , distance ) ) ; \n"

"CREATE INDEX trmpath_predicateid ON term_path ( predicate_term_id ); \n"
"CREATE INDEX trmpath_objectid ON term_path ( object_term_id ); \n"
"CREATE INDEX trmpath_ontid ON term_path ( ontology_id ); \n"

"CREATE SEQUENCE bioentry_pk_seq;\n"
"CREATE TABLE bioentry ( \n"
"	 bioentry_id INTEGER DEFAULT nextval ( 'bioentry_pk_seq' ) NOT NULL , \n"
"	 biodatabase_id INTEGER NOT NULL , \n"
"	 taxon_id INTEGER , \n"
"	 name VARCHAR ( 40 ) NOT NULL , \n"
"	 accession VARCHAR ( 128 ) NOT NULL , \n"
"	 identifier VARCHAR ( 40 ) , \n"
"	 division VARCHAR ( 6 ) , \n"
"	 description TEXT , \n"
"	 version INTEGER NOT NULL , \n"
"	 PRIMARY KEY ( bioentry_id ) , \n"
"	 UNIQUE ( accession , biodatabase_id , version ) , \n"
"	 UNIQUE ( identifier , biodatabase_id ) \n"
") ; \n"

"CREATE INDEX bioentry_name ON bioentry ( name ); \n"
"CREATE INDEX bioentry_db ON bioentry ( biodatabase_id ); \n"
"CREATE INDEX bioentry_tax ON bioentry ( taxon_id ); \n"

"CREATE SEQUENCE bioentry_relationship_pk_seq;\n"
"CREATE TABLE bioentry_relationship ( \n"
"	 bioentry_relationship_id INTEGER DEFAULT nextval ( 'bioentry_relationship_pk_seq' ) NOT NULL , \n"
"	 object_bioentry_id INTEGER NOT NULL , \n"
"	 subject_bioentry_id INTEGER NOT NULL , \n"
"	 term_id INTEGER NOT NULL , \n"
"	 rank INTEGER , \n"
"	 PRIMARY KEY ( bioentry_relationship_id ) , \n"
"	 UNIQUE ( object_bioentry_id , subject_bioentry_id , term_id ) ) ; \n"

"CREATE INDEX bioentryrel_trm ON bioentry_relationship ( term_id ); \n"
"CREATE INDEX bioentryrel_child ON bioentry_relationship (subject_bioentry_id);\n"

"CREATE TABLE bioentry_path ( \n"
"	 object_bioentry_id INTEGER NOT NULL , \n"
"	 subject_bioentry_id INTEGER NOT NULL , \n"
"	 term_id INTEGER NOT NULL , \n"
"	 distance INTEGER,\n"
"	 UNIQUE ( object_bioentry_id , subject_bioentry_id , term_id , distance ) ) ; \n"

"CREATE INDEX bioentrypath_trm ON bioentry_path ( term_id ); \n"
"CREATE INDEX bioentrypath_child ON bioentry_path ( subject_bioentry_id ); \n"

"CREATE TABLE biosequence ( \n"
"	 bioentry_id INTEGER NOT NULL , \n"
"	 version INTEGER , \n"
"	 length INTEGER , \n"
"	 alphabet VARCHAR ( 10 ) , \n"
"	 seq TEXT , \n"
"	 PRIMARY KEY ( bioentry_id ) ) ; \n"

"CREATE SEQUENCE dbxref_pk_seq;\n"
"CREATE TABLE dbxref ( \n"
"	 dbxref_id INTEGER DEFAULT nextval ( 'dbxref_pk_seq' ) NOT NULL , \n"
"	 dbname VARCHAR ( 40 ) NOT NULL , \n"
"	 accession VARCHAR ( 128 ) NOT NULL , \n"
"	 version INTEGER NOT NULL , \n"
"	 PRIMARY KEY ( dbxref_id ) , \n"
"	 UNIQUE ( accession , dbname , version ) ) ; \n"

"CREATE INDEX dbxref_db ON dbxref ( dbname ); \n"

"CREATE TABLE dbxref_qualifier_value ( \n"
"	 dbxref_id INTEGER NOT NULL , \n"
"	 term_id INTEGER NOT NULL , \n"
"	 rank INTEGER NOT NULL DEFAULT 0 , \n"
"	 value TEXT , \n"
"	 PRIMARY KEY ( dbxref_id , term_id , rank ) ) ; \n"

"CREATE INDEX dbxrefqual_dbx ON dbxref_qualifier_value ( dbxref_id ); \n"
"CREATE INDEX dbxrefqual_trm ON dbxref_qualifier_value ( term_id ); \n"

"CREATE TABLE bioentry_dbxref ( \n"
"	 bioentry_id INTEGER NOT NULL , \n"
"	 dbxref_id INTEGER NOT NULL , \n"
"	 rank INTEGER , \n"
"	 PRIMARY KEY ( bioentry_id , dbxref_id ) ) ; \n"

"CREATE INDEX dblink_dbx ON bioentry_dbxref ( dbxref_id ); \n"

"CREATE SEQUENCE reference_pk_seq;\n"
"CREATE TABLE reference ( \n"
"	 reference_id INTEGER DEFAULT nextval ( 'reference_pk_seq' ) NOT NULL , \n"
"	 dbxref_id INTEGER , \n"
"	 location TEXT NOT NULL , \n"
"	 title TEXT , \n"
"	 authors TEXT , \n"
"	 crc VARCHAR ( 32 ) , \n"
"	 PRIMARY KEY ( reference_id ) , \n"
"	 UNIQUE ( dbxref_id ) , \n"
"	 UNIQUE ( crc ) ) ; \n"

"CREATE TABLE bioentry_reference ( \n"
"	 bioentry_id INTEGER NOT NULL , \n"
"	 reference_id INTEGER NOT NULL , \n"
"	 start_pos INTEGER , \n"
"	 end_pos INTEGER , \n"
"	 rank INTEGER NOT NULL DEFAULT 0 , \n"
"	 PRIMARY KEY ( bioentry_id , reference_id , rank ) ) ; \n"

"CREATE INDEX bioentryref_ref ON bioentry_reference ( reference_id ); \n"

"CREATE SEQUENCE comment_pk_seq;\n"
"CREATE TABLE comment ( \n"
"	 comment_id INTEGER DEFAULT nextval ( 'comment_pk_seq' ) NOT NULL , \n"
"	 bioentry_id INTEGER NOT NULL , \n"
"	 comment_text TEXT NOT NULL , \n"
"	 rank INTEGER NOT NULL DEFAULT 0 , \n"
"	 PRIMARY KEY ( comment_id ) , \n"
"	 UNIQUE ( bioentry_id , rank ) ) ; \n"

"CREATE TABLE bioentry_qualifier_value ( \n"
"	 bioentry_id INTEGER NOT NULL , \n"
"	 term_id INTEGER NOT NULL , \n"
"	 value TEXT , \n"
"	 rank INTEGER NOT NULL DEFAULT 0 , \n"
"	 UNIQUE ( bioentry_id , term_id , rank ) ) ; \n"

"CREATE INDEX bioentryqual_trm ON bioentry_qualifier_value ( term_id ); \n"

"CREATE SEQUENCE seqfeature_pk_seq;\n"
"CREATE TABLE seqfeature ( \n"
"	 seqfeature_id INTEGER DEFAULT nextval ( 'seqfeature_pk_seq' ) NOT NULL , \n"
"	 bioentry_id INTEGER NOT NULL , \n"
"	 type_term_id INTEGER NOT NULL , \n"
"	 source_term_id INTEGER NOT NULL , \n"
"	 display_name VARCHAR ( 64 ) , \n"
"	 rank INTEGER NOT NULL DEFAULT 0 , \n"
"	 PRIMARY KEY ( seqfeature_id ) , \n"
"	 UNIQUE ( bioentry_id , type_term_id , source_term_id , rank ) ) ; \n"

"CREATE INDEX seqfeature_trm ON seqfeature ( type_term_id ); \n"
"CREATE INDEX seqfeature_fsrc ON seqfeature ( source_term_id ); \n"

"CREATE SEQUENCE seqfeature_relationship_pk_seq;\n"
"CREATE TABLE seqfeature_relationship ( \n"
"	 seqfeature_relationship_id INTEGER DEFAULT nextval ( 'seqfeature_relationship_pk_seq' ) NOT NULL , \n"
"	 object_seqfeature_id INTEGER NOT NULL , \n"
"	 subject_seqfeature_id INTEGER NOT NULL , \n"
"	 term_id INTEGER NOT NULL , \n"
"	 rank INTEGER , \n"
"	 PRIMARY KEY ( seqfeature_relationship_id ) , \n"
"	 UNIQUE ( object_seqfeature_id , subject_seqfeature_id , term_id ) ) ; \n"

"CREATE INDEX seqfeaturerel_trm ON seqfeature_relationship ( term_id ); \n"
"CREATE INDEX seqfeaturerel_child ON seqfeature_relationship ( subject_seqfeature_id ); \n"

"CREATE TABLE seqfeature_path ( \n"
"	 object_seqfeature_id INTEGER NOT NULL , \n"
"	 subject_seqfeature_id INTEGER NOT NULL , \n"
"	 term_id INTEGER NOT NULL , \n"
"	 distance INTEGER,\n"
"	 UNIQUE ( object_seqfeature_id , subject_seqfeature_id , term_id , distance ) ) ; \n"

"CREATE INDEX seqfeaturepath_trm ON seqfeature_path ( term_id ); \n"
"CREATE INDEX seqfeaturepath_child ON seqfeature_path ( subject_seqfeature_id );\n"

"CREATE TABLE seqfeature_qualifier_value ( \n"
"	 seqfeature_id INTEGER NOT NULL , \n"
"	 term_id INTEGER NOT NULL , \n"
"	 rank INTEGER NOT NULL DEFAULT 0 , \n"
"	 value TEXT NOT NULL , \n"
"	 PRIMARY KEY ( seqfeature_id , term_id , rank ) ) ; \n"

"CREATE INDEX seqfeaturequal_trm ON seqfeature_qualifier_value ( term_id ); \n"

"CREATE TABLE seqfeature_dbxref ( \n"
"	 seqfeature_id INTEGER NOT NULL , \n"
"	 dbxref_id INTEGER NOT NULL , \n"
"	 rank INTEGER , \n"
"	 PRIMARY KEY ( seqfeature_id , dbxref_id ) ) ; \n"

"CREATE INDEX feadblink_dbx ON seqfeature_dbxref ( dbxref_id ); \n"

"CREATE SEQUENCE location_pk_seq;\n"
"CREATE TABLE location ( \n"
"	 location_id INTEGER DEFAULT nextval ( 'location_pk_seq' ) NOT NULL , \n"
"	 seqfeature_id INTEGER NOT NULL , \n"
"	 dbxref_id INTEGER , \n"
"	 term_id INTEGER , \n"
"	 start_pos INTEGER , \n"
"	 end_pos INTEGER , \n"
"	 strand INTEGER NOT NULL DEFAULT 0 , \n"
"	 rank INTEGER NOT NULL DEFAULT 0 , \n"
"	 PRIMARY KEY ( location_id ) , \n"
"	 UNIQUE ( seqfeature_id , rank ) ) ; \n"

"CREATE INDEX seqfeatureloc_start ON location ( start_pos, end_pos ); \n"
"CREATE INDEX seqfeatureloc_dbx ON location ( dbxref_id ); \n"
"CREATE INDEX seqfeatureloc_trm ON location ( term_id ); \n"

"CREATE TABLE location_qualifier_value ( \n"
"	 location_id INTEGER NOT NULL , \n"
"	 term_id INTEGER NOT NULL , \n"
"	 value VARCHAR ( 255 ) NOT NULL , \n"
"	 int_value INTEGER , \n"
"	 PRIMARY KEY ( location_id , term_id ) ) ; \n"

"CREATE INDEX locationqual_trm ON location_qualifier_value ( term_id ); \n"

"ALTER TABLE term ADD CONSTRAINT FKont_term\n"
"      FOREIGN KEY ( ontology_id ) REFERENCES ontology ( ontology_id ) \n"
"      ON DELETE CASCADE ;\n"

"ALTER TABLE term_synonym ADD CONSTRAINT FKterm_syn\n"
"      FOREIGN KEY ( term_id ) REFERENCES term ( term_id )\n"
"      ON DELETE CASCADE ;\n"

"ALTER TABLE term_dbxref ADD CONSTRAINT FKdbxref_trmdbxref\n"
"      FOREIGN KEY ( dbxref_id ) REFERENCES dbxref ( dbxref_id )\n"
"      ON DELETE CASCADE ;\n"
"ALTER TABLE term_dbxref ADD CONSTRAINT FKterm_trmdbxref\n"
"      FOREIGN KEY ( term_id ) REFERENCES term ( term_id )\n"
"      ON DELETE CASCADE ;\n"

"ALTER TABLE term_relationship ADD CONSTRAINT FKtrmsubject_trmrel\n"
"      FOREIGN KEY ( subject_term_id ) REFERENCES term ( term_id )\n"
"      ON DELETE CASCADE ;\n"
"ALTER TABLE term_relationship ADD CONSTRAINT FKtrmpredicate_trmrel\n"
"      FOREIGN KEY ( predicate_term_id ) REFERENCES term ( term_id )\n"
"      ON DELETE CASCADE ;\n"
"ALTER TABLE term_relationship ADD CONSTRAINT FKtrmobject_trmrel\n"
"      FOREIGN KEY ( object_term_id ) REFERENCES term ( term_id )\n"
"      ON DELETE CASCADE ;\n"
"ALTER TABLE term_relationship ADD CONSTRAINT FKontology_trmrel\n"
"      FOREIGN KEY ( ontology_id ) REFERENCES ontology ( ontology_id )\n"
"      ON DELETE CASCADE ;\n"

"ALTER TABLE term_relationship_term ADD CONSTRAINT FKtrmrel_trmreltrm\n"
"      FOREIGN KEY (term_relationship_id) REFERENCES term_relationship(term_relationship_id)\n"
"      ON DELETE CASCADE ;\n"
"ALTER TABLE term_relationship_term ADD CONSTRAINT FKtrm_trmreltrm\n"
"      FOREIGN KEY (term_id) REFERENCES term(term_id)\n"
"      ON DELETE CASCADE ;\n"

"ALTER TABLE term_path ADD CONSTRAINT FKtrmsubject_trmpath\n"
"      FOREIGN KEY ( subject_term_id ) REFERENCES term ( term_id )\n"
"      ON DELETE CASCADE ;\n"
"ALTER TABLE term_path ADD CONSTRAINT FKtrmpredicate_trmpath\n"
"      FOREIGN KEY ( predicate_term_id ) REFERENCES term ( term_id )\n"
"      ON DELETE CASCADE ;\n"
"ALTER TABLE term_path ADD CONSTRAINT FKtrmobject_trmpath\n"
"      FOREIGN KEY ( object_term_id ) REFERENCES term ( term_id )\n"
"      ON DELETE CASCADE ;\n"
"ALTER TABLE term_path ADD CONSTRAINT FKontology_trmpath\n"
"      FOREIGN KEY ( ontology_id ) REFERENCES ontology ( ontology_id )\n"
"      ON DELETE CASCADE ;\n"

"ALTER TABLE taxon_name ADD CONSTRAINT FKtaxon_taxonname\n"
"      FOREIGN KEY ( taxon_id ) REFERENCES taxon ( taxon_id )\n"
"      ON DELETE CASCADE ;\n"

"ALTER TABLE bioentry ADD CONSTRAINT FKtaxon_bioentry\n"
"      FOREIGN KEY ( taxon_id ) REFERENCES taxon ( taxon_id ) ; \n"
"ALTER TABLE bioentry ADD CONSTRAINT FKbiodatabase_bioentry\n"
"      FOREIGN KEY ( biodatabase_id ) REFERENCES biodatabase ( biodatabase_id ) ; \n"

"ALTER TABLE bioentry_relationship ADD CONSTRAINT FKterm_bioentryrel\n"
"      FOREIGN KEY ( term_id ) REFERENCES term ( term_id ) ; \n"
"ALTER TABLE bioentry_relationship ADD CONSTRAINT FKparentent_bioentryrel\n"
"      FOREIGN KEY ( object_bioentry_id ) REFERENCES bioentry ( bioentry_id )\n"
"      ON DELETE CASCADE ;\n"
"ALTER TABLE bioentry_relationship ADD CONSTRAINT FKchildent_bioentryrel\n"
"      FOREIGN KEY ( subject_bioentry_id ) REFERENCES bioentry ( bioentry_id )\n"
"      ON DELETE CASCADE ;\n"

"ALTER TABLE bioentry_path ADD CONSTRAINT FKterm_bioentrypath\n"
"      FOREIGN KEY ( term_id ) REFERENCES term ( term_id ) ; \n"
"ALTER TABLE bioentry_path ADD CONSTRAINT FKparentent_bioentrypath\n"
"      FOREIGN KEY ( object_bioentry_id ) REFERENCES bioentry ( bioentry_id )\n"
"      ON DELETE CASCADE ;\n"
"ALTER TABLE bioentry_path ADD CONSTRAINT FKchildent_bioentrypath\n"
"      FOREIGN KEY ( subject_bioentry_id ) REFERENCES bioentry ( bioentry_id )\n"
"      ON DELETE CASCADE ;\n"

"ALTER TABLE biosequence ADD CONSTRAINT FKbioentry_bioseq\n"
"      FOREIGN KEY ( bioentry_id ) REFERENCES bioentry ( bioentry_id )\n"
"      ON DELETE CASCADE ;\n"

"ALTER TABLE comment ADD CONSTRAINT FKbioentry_comment\n"
"      FOREIGN KEY ( bioentry_id ) REFERENCES bioentry ( bioentry_id )\n"
"      ON DELETE CASCADE ;\n"

"ALTER TABLE bioentry_dbxref ADD CONSTRAINT FKbioentry_dblink\n"
"      FOREIGN KEY ( bioentry_id ) REFERENCES bioentry ( bioentry_id )\n"
"      ON DELETE CASCADE ;\n"
"ALTER TABLE bioentry_dbxref ADD CONSTRAINT FKdbxref_dblink\n"
"      FOREIGN KEY ( dbxref_id ) REFERENCES dbxref ( dbxref_id )\n"
"      ON DELETE CASCADE ;\n"

"ALTER TABLE dbxref_qualifier_value ADD CONSTRAINT FKtrm_dbxrefqual\n"
"      FOREIGN KEY ( term_id ) REFERENCES term ( term_id ) ;\n"
"ALTER TABLE dbxref_qualifier_value ADD CONSTRAINT FKdbxref_dbxrefqual\n"
"      FOREIGN KEY ( dbxref_id ) REFERENCES dbxref ( dbxref_id )\n"
"      ON DELETE CASCADE ;\n"

"ALTER TABLE bioentry_reference ADD CONSTRAINT FKbioentry_entryref\n"
"      FOREIGN KEY ( bioentry_id ) REFERENCES bioentry ( bioentry_id )\n"
"      ON DELETE CASCADE ;\n"
"ALTER TABLE bioentry_reference ADD CONSTRAINT FKreference_entryref\n"
"      FOREIGN KEY ( reference_id ) REFERENCES reference ( reference_id )\n"
"      ON DELETE CASCADE ;\n"

"ALTER TABLE reference ADD CONSTRAINT FKdbxref_reference\n"
"      FOREIGN KEY ( dbxref_id ) REFERENCES dbxref ( dbxref_id ) ;\n"

"ALTER TABLE bioentry_qualifier_value ADD CONSTRAINT FKbioentry_entqual\n"
"      FOREIGN KEY ( bioentry_id ) REFERENCES bioentry ( bioentry_id )\n"
"      ON DELETE CASCADE ;\n"
"ALTER TABLE bioentry_qualifier_value ADD CONSTRAINT FKterm_entqual\n"
"      FOREIGN KEY ( term_id ) REFERENCES term ( term_id ) ;\n"

"ALTER TABLE seqfeature ADD CONSTRAINT FKterm_seqfeature\n"
"      FOREIGN KEY ( type_term_id ) REFERENCES term ( term_id ) ; \n"
"ALTER TABLE seqfeature ADD CONSTRAINT FKsourceterm_seqfeature\n"
"      FOREIGN KEY ( source_term_id ) REFERENCES term ( term_id ) ; \n"
"ALTER TABLE seqfeature ADD CONSTRAINT FKbioentry_seqfeature\n"
"      FOREIGN KEY ( bioentry_id ) REFERENCES bioentry ( bioentry_id )\n"
"      ON DELETE CASCADE ;\n"

"ALTER TABLE seqfeature_relationship ADD CONSTRAINT FKterm_seqfeatrel\n"
"      FOREIGN KEY ( term_id ) REFERENCES term ( term_id ) ;\n"
"ALTER TABLE seqfeature_relationship ADD CONSTRAINT FKparentfeat_seqfeatrel\n"
"      FOREIGN KEY ( object_seqfeature_id ) REFERENCES seqfeature ( seqfeature_id )\n"
"      ON DELETE CASCADE ;\n"
"ALTER TABLE seqfeature_relationship ADD CONSTRAINT FKchildfeat_seqfeatrel\n"
"      FOREIGN KEY ( subject_seqfeature_id ) REFERENCES seqfeature ( seqfeature_id )\n"
"      ON DELETE CASCADE ;\n"

"ALTER TABLE seqfeature_path ADD CONSTRAINT FKterm_seqfeatpath\n"
"      FOREIGN KEY ( term_id ) REFERENCES term ( term_id ) ;\n"
"ALTER TABLE seqfeature_path ADD CONSTRAINT FKparentfeat_seqfeatpath\n"
"      FOREIGN KEY ( object_seqfeature_id ) REFERENCES seqfeature ( seqfeature_id )\n"
"      ON DELETE CASCADE ;\n"
"ALTER TABLE seqfeature_path ADD CONSTRAINT FKchildfeat_seqfeatpath\n"
"      FOREIGN KEY ( subject_seqfeature_id ) REFERENCES seqfeature ( seqfeature_id )\n"
"      ON DELETE CASCADE ;\n"

"ALTER TABLE seqfeature_qualifier_value ADD CONSTRAINT FKterm_featqual\n"
"      FOREIGN KEY ( term_id ) REFERENCES term ( term_id ) ;\n"
"ALTER TABLE seqfeature_qualifier_value ADD CONSTRAINT FKseqfeature_featqual\n"
"      FOREIGN KEY ( seqfeature_id ) REFERENCES seqfeature ( seqfeature_id )\n"
"      ON DELETE CASCADE ;\n"

"ALTER TABLE seqfeature_dbxref ADD CONSTRAINT FKseqfeature_feadblink\n"
"      FOREIGN KEY ( seqfeature_id ) REFERENCES seqfeature ( seqfeature_id )\n"
"      ON DELETE CASCADE ;\n"
"ALTER TABLE seqfeature_dbxref ADD CONSTRAINT FKdbxref_feadblink\n"
"      FOREIGN KEY ( dbxref_id ) REFERENCES dbxref ( dbxref_id )\n"
"      ON DELETE CASCADE ;\n"

"ALTER TABLE location ADD CONSTRAINT FKseqfeature_location\n"
"      FOREIGN KEY ( seqfeature_id ) REFERENCES seqfeature ( seqfeature_id )\n"
"      ON DELETE CASCADE ;\n"
"ALTER TABLE location ADD CONSTRAINT FKdbxref_location\n"
"      FOREIGN KEY ( dbxref_id ) REFERENCES dbxref ( dbxref_id ) ;\n"
"ALTER TABLE location ADD CONSTRAINT FKterm_featloc\n"
"      FOREIGN KEY ( term_id ) REFERENCES term ( term_id ) ;\n"

"ALTER TABLE location_qualifier_value ADD CONSTRAINT FKfeatloc_locqual\n"
"      FOREIGN KEY ( location_id ) REFERENCES location ( location_id )\n"
"      ON DELETE CASCADE ;\n"
"ALTER TABLE location_qualifier_value ADD CONSTRAINT FKterm_locqual\n"
"      FOREIGN KEY ( term_id ) REFERENCES term ( term_id ) ;\n"

"CREATE RULE rule_bioentry_i1\n"
"       AS ON INSERT TO bioentry\n"
"       WHERE (\n"
"             SELECT bioentry_id FROM bioentry\n"
"             WHERE identifier     = new.identifier\n"
"             AND   biodatabase_id = new.biodatabase_id\n"
"             ) \n"
"	     IS NOT NULL\n"
"       DO INSTEAD NOTHING\n"
";\n"
"CREATE RULE rule_bioentry_i2\n"
"       AS ON INSERT TO bioentry\n"
"       WHERE (\n"
"       	     SELECT bioentry_id FROM bioentry\n"
"	     WHERE accession      = new.accession\n"
"	     AND   biodatabase_id = new.biodatabase_id\n"
"	     AND   version	  = new.version\n"
"	     ) \n"
"	     IS NOT NULL\n"
"       DO INSTEAD NOTHING\n"
";\n"

"CREATE RULE rule_biodatabase_i\n"
"       AS ON INSERT TO biodatabase\n"
"       WHERE (\n"
"             SELECT biodatabase_id FROM biodatabase \n"
"             WHERE name = new.name\n"
"             )\n"
"       	     IS NOT NULL\n"
"       DO INSTEAD NOTHING\n"
";\n"

"CREATE RULE rule_bioentry_dbxref_i\n"
"       AS ON INSERT TO bioentry_dbxref\n"
"       WHERE (\n"
"       	     SELECT dbxref_id FROM bioentry_dbxref\n"
"	     WHERE bioentry_id = new.bioentry_id\n"
"	     AND   dbxref_id   = new.dbxref_id\n"
"	     ) \n"
"	     IS NOT NULL\n"
"       DO INSTEAD NOTHING\n"
";\n"

"CREATE RULE rule_bioentry_path_i\n"
"       AS ON INSERT TO bioentry_path\n"
"       WHERE (\n"
"       	     SELECT bioentry_relationship_id FROM bioentry_relationship\n"
"	     WHERE object_bioentry_id = new.object_bioentry_id\n"
"	     AND   subject_bioentry_id= new.subject_bioentry_id\n"
"	     AND   term_id	      = new.term_id\n"
"	     )\n"
"	     IS NOT NULL\n"
"       DO INSTEAD NOTHING\n"
";\n"

"CREATE RULE rule_bioentry_qualifier_value_i\n"
"       AS ON INSERT TO bioentry_qualifier_value\n"
"       WHERE (\n"
"       	     SELECT bioentry_id FROM bioentry_qualifier_value\n"
"	     WHERE bioentry_id = new.bioentry_id\n"
"	     AND   term_id     = new.term_id\n"
"	     AND   rank	       = new.rank\n"
"	     )\n"
"	     IS NOT NULL\n"
"       DO INSTEAD NOTHING\n"
";\n"

"CREATE RULE rule_bioentry_reference_i\n"
"       AS ON INSERT TO bioentry_reference\n"
"       WHERE (\n"
"       	     SELECT bioentry_id FROM bioentry_reference \n"
"	     WHERE bioentry_id  = new.bioentry_id\n"
"	     AND   reference_id = new.reference_id\n"
"	     AND   rank		= new.rank\n"
"	     )\n"
"	     IS NOT NULL\n"
"       DO INSTEAD NOTHING\n"
";\n"

"CREATE RULE rule_bioentry_relationship_i\n"
"       AS ON INSERT TO bioentry_relationship\n"
"       WHERE (\n"
"       	     SELECT bioentry_relationship_id FROM bioentry_relationship\n"
"	     WHERE object_bioentry_id = new.object_bioentry_id\n"
"	     AND   subject_bioentry_id= new.subject_bioentry_id\n"
"	     AND   term_id	      = new.term_id\n"
"	     )\n"
"	     IS NOT NULL\n"
"       DO INSTEAD NOTHING\n"
";\n"

"CREATE RULE rule_biosequence_i\n"
"       AS ON INSERT TO biosequence\n"
"       WHERE (\n"
"             SELECT bioentry_id FROM biosequence \n"
"             WHERE bioentry_id = new.bioentry_id\n"
"             )\n"
"       	     IS NOT NULL\n"
"       DO INSTEAD NOTHING\n"
";\n"

"CREATE RULE rule_comment_i\n"
"       AS ON INSERT TO comment\n"
"       WHERE (\n"
"       	     SELECT comment_id FROM comment\n"
"	     WHERE bioentry_id = new.bioentry_id\n"
"	     AND   rank	       = new.rank\n"
"	     )\n"
"	     IS NOT NULL\n"
"       DO INSTEAD NOTHING\n"
";\n"

"CREATE RULE rule_dbxref_i\n"
"       AS ON INSERT TO dbxref\n"
"       WHERE (\n"
"       	     SELECT dbxref_id FROM dbxref\n"
"	     WHERE accession = new.accession\n"
"	     AND   dbname    = new.dbname\n"
"	     AND   version   = new.version\n"
"	     )\n"
"	     IS NOT NULL\n"
"       DO INSTEAD NOTHING\n"
";\n"

"CREATE RULE rule_dbxref_qualifier_value_i\n"
"       AS ON INSERT TO dbxref_qualifier_value\n"
"       WHERE (\n"
"       	     SELECT dbxref_id FROM dbxref_qualifier_value\n"
"	     WHERE dbxref_id = new.dbxref_id\n"
"	     AND   term_id   = new.term_id\n"
"	     AND   rank	     = new.rank\n"
"	     )\n"
"	     IS NOT NULL\n"
"       DO INSTEAD NOTHING\n"
";\n"

"CREATE RULE rule_location_i\n"
"       AS ON INSERT TO location\n"
"       WHERE (\n"
"       	     SELECT location_id FROM location\n"
"	     WHERE seqfeature_id = new.seqfeature_id\n"
"	     AND   rank		 = new.rank\n"
"	     )\n"
"	     IS NOT NULL\n"
"       DO INSTEAD NOTHING\n"
";\n"

"CREATE RULE rule_location_qualifier_value_i\n"
"       AS ON INSERT TO location_qualifier_value\n"
"       WHERE (\n"
"       	     SELECT location_id FROM location_qualifier_value\n"
"	     WHERE location_id = new.location_id\n"
"	     AND   term_id     = new.term_id\n"
"	     ) \n"
"       	     IS NOT NULL\n"
"       DO INSTEAD NOTHING\n"
";\n"

"CREATE RULE rule_ontology_i\n"
"       AS ON INSERT TO ontology\n"
"       WHERE (\n"
"             SELECT ontology_id FROM ontology \n"
"             WHERE name = new.name\n"
"             ) \n"
"       	     IS NOT NULL\n"
"       DO INSTEAD NOTHING\n"
";\n"

"CREATE RULE rule_reference_i1\n"
"       AS ON INSERT TO reference\n"
"       WHERE (\n"
"             SELECT reference_id FROM reference \n"
"             WHERE crc = new.crc\n"
"             ) \n"
"       	     IS NOT NULL\n"
"       DO INSTEAD NOTHING\n"
";\n"
"CREATE RULE rule_reference_i2\n"
"       AS ON INSERT TO reference\n"
"       WHERE (\n"
"             SELECT reference_id FROM reference\n"
"             WHERE dbxref_id = new.dbxref_id\n"
"             )\n"
"       	     IS NOT NULL\n"
"       DO INSTEAD NOTHING\n"
";\n"

"CREATE RULE rule_seqfeature_i\n"
"       AS ON INSERT TO seqfeature\n"
"       WHERE (\n"
"       	     SELECT seqfeature_id FROM seqfeature \n"
"	     WHERE bioentry_id    = new.bioentry_id\n"
"	     AND   type_term_id   = new.type_term_id\n"
"	     AND   source_term_id = new.source_term_id\n"
"	     AND   rank		  = new.rank\n"
"	     )\n"
"	     IS NOT NULL\n"
"       DO INSTEAD NOTHING\n"
";\n"

"CREATE RULE rule_seqfeature_dbxref_i\n"
"       AS ON INSERT TO seqfeature_dbxref\n"
"       WHERE (	    \n"
"       	     SELECT seqfeature_id FROM seqfeature_dbxref\n"
"	     WHERE seqfeature_id = new.seqfeature_id\n"
"	     AND   dbxref_id	 = new.dbxref_id\n"
"	     )\n"
"	     IS NOT NULL\n"
"       DO INSTEAD NOTHING\n"
";\n"

"CREATE RULE rule_seqfeature_path_i\n"
"       AS ON INSERT TO seqfeature_path\n"
"       WHERE (\n"
"       	     SELECT subject_seqfeature_id FROM seqfeature_path\n"
"	     WHERE object_seqfeature_id = new.object_seqfeature_id\n"
"	     AND   subject_seqfeature_id= new.subject_seqfeature_id\n"
"	     AND   term_id		= new.term_id\n"
"	     )\n"
"	     IS NOT NULL\n"
"       DO INSTEAD NOTHING\n"
";\n"

"CREATE RULE rule_seqfeature_qualifier_value_i\n"
"       AS ON INSERT TO seqfeature_qualifier_value\n"
"       WHERE (\n"
"       	     SELECT seqfeature_id FROM seqfeature_qualifier_value\n"
"	     WHERE seqfeature_id = new.seqfeature_id\n"
"	     AND   term_id	 = new.term_id\n"
"	     AND   rank		 = new.rank\n"
"	     )\n"
"	     IS NOT NULL\n"
"       DO INSTEAD NOTHING\n"
";\n"

"CREATE RULE rule_seqfeature_relationship_i\n"
"       AS ON INSERT TO seqfeature_relationship\n"
"       WHERE (\n"
"       	     SELECT subject_seqfeature_id FROM seqfeature_relationship\n"
"	     WHERE object_seqfeature_id = new.object_seqfeature_id\n"
"	     AND   subject_seqfeature_id= new.subject_seqfeature_id\n"
"	     AND   term_id		= new.term_id\n"
"	     )\n"
"	     IS NOT NULL\n"
"       DO INSTEAD NOTHING\n"
";\n"

"CREATE RULE rule_taxon_i\n"
"       AS ON INSERT TO taxon\n"
"       WHERE (\n"
"             SELECT taxon_id FROM taxon \n"
"             WHERE ncbi_taxon_id = new.ncbi_taxon_id\n"
"             )\n"
"       	     IS NOT NULL\n"
"       DO INSTEAD NOTHING\n"
";\n"

"CREATE RULE rule_taxon_name_i\n"
"       AS ON INSERT TO taxon_name\n"
"       WHERE (\n"
"       	     SELECT taxon_id FROM taxon_name\n"
"	     WHERE taxon_id   = new.taxon_id\n"
"	     AND   name	      = new.name\n"
"	     AND   name_class = new.name_class\n"
"	     ) \n"
"	     IS NOT NULL\n"
"       DO INSTEAD NOTHING\n"
";\n"

"CREATE RULE rule_term_i1\n"
"       AS ON INSERT TO term\n"
"       WHERE (\n"
"             SELECT term_id FROM term\n"
"             WHERE identifier = new.identifier\n"
"             )\n"
"       	     IS NOT NULL\n"
"       DO INSTEAD NOTHING\n"
";\n"
"CREATE RULE rule_term_i2\n"
"       AS ON INSERT TO term\n"
"       WHERE (\n"
"       	     SELECT term_id FROM term\n"
"	     WHERE name        = new.name\n"
"	     AND   ontology_id = new.ontology_id\n"
"             AND   is_obsolete = new.is_obsolete\n"
"	     )\n"
"       	     IS NOT NULL\n"
"       DO INSTEAD NOTHING\n"
";\n"

"CREATE RULE rule_term_dbxref_i\n"
"       AS ON INSERT TO term_dbxref\n"
"       WHERE (\n"
"       	     SELECT dbxref_id FROM term_dbxref\n"
"	     WHERE dbxref_id = new.dbxref_id\n"
"	     AND   term_id   = new.term_id\n"
"	     )\n"
"	     IS NOT NULL\n"
"       DO INSTEAD NOTHING\n"
";\n"

"CREATE RULE rule_term_path_i\n"
"       AS ON INSERT TO term_path\n"
"       WHERE (\n"
"       	     SELECT subject_term_id FROM term_path\n"
"	     WHERE subject_term_id   = new.subject_term_id\n"
"	     AND   predicate_term_id = new.predicate_term_id\n"
"	     AND   object_term_id    = new.object_term_id\n"
"	     AND   ontology_id	     = new.ontology_id\n"
"	     AND   distance	     = new.distance\n"
"	     )\n"
"	     IS NOT NULL\n"
"       DO INSTEAD NOTHING\n"
";\n"

"CREATE RULE rule_term_relationship_i\n"
"       AS ON INSERT TO term_relationship\n"
"       WHERE (\n"
"       	     SELECT term_relationship_id FROM term_relationship\n"
"	     WHERE subject_term_id   = new.subject_term_id\n"
"	     AND   predicate_term_id = new.predicate_term_id\n"
"	     AND   object_term_id    = new.object_term_id\n"
"	     AND   ontology_id	     = new.ontology_id\n"
"	     )\n"
"	     IS NOT NULL\n"
"       DO INSTEAD NOTHING\n"
";\n"

"CREATE RULE rule_term_relationship_term_i1\n"
"       AS ON INSERT TO term_relationship_term\n"
"       WHERE (\n"
"       	     SELECT term_relationship_id FROM term_relationship_term\n"
"	     WHERE term_relationship_id   = new.term_relationship_id\n"
"	     )\n"
"	     IS NOT NULL\n"
"       DO INSTEAD NOTHING\n"
";\n"

"CREATE RULE rule_term_relationship_term_i2\n"
"       AS ON INSERT TO term_relationship_term\n"
"       WHERE (\n"
"       	     SELECT term_id FROM term_relationship_term\n"
"	     WHERE term_id   = new.term_id\n"
"	     )\n"
"	     IS NOT NULL\n"
"       DO INSTEAD NOTHING\n"
";\n"

"CREATE RULE rule_term_synonym_i\n"
"       AS ON INSERT TO term_synonym\n"
"       WHERE (\n"
"       	     SELECT term_id FROM term_synonym\n"
"	     WHERE synonym = new.synonym\n"
"	     AND   term_id = new.term_id\n"
"	     )\n"
"	     IS NOT NULL\n"
"       DO INSTEAD NOTHING\n"
";\n"

"CREATE OR REPLACE FUNCTION unconstrain_taxon ()\n"
"RETURNS INTEGER\n"
"AS\n"
"'\n"
"DROP RULE rule_taxon_i ON taxon;\n"
"SELECT 1;\n"
"'\n"
"LANGUAGE SQL\n"
"VOLATILE STRICT SECURITY DEFINER\n"
";\n"

"CREATE OR REPLACE FUNCTION constrain_taxon ()\n"
"RETURNS INTEGER\n"
"AS\n"
"'\n"
"CREATE RULE rule_taxon_i\n"
"       AS ON INSERT TO taxon\n"
"       WHERE (\n"
"             SELECT taxon_id FROM taxon \n"
"             WHERE ncbi_taxon_id = new.ncbi_taxon_id\n"
"             )\n"
"       	     IS NOT NULL\n"
"       DO INSTEAD NOTHING\n"
";\n"
"SELECT 1;\n"
"'\n"
"LANGUAGE SQL\n"
"VOLATILE STRICT SECURITY DEFINER\n"
";";
